最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get data (json format) from another domain without jsonp - Stack Overflow

programmeradmin3浏览0评论

How can i get data (json format) from another domain?
My problem is: I want to get data from: /
I have tried to use CORS but it didn't work.
My console is:
GET [HTTP/1.1 200 OK 780ms]
Error Error
My code by client-script:

$(document).ready(function() {
 $("#get_data_btn").click(function() {
   var data_path = "";
   $.getJSON(data_path, null)
    .done(function(data) {
      console.log("Success: " + data.status);
    })
    .fail(function(jqxhr, textStatus, error) {
      console.log("Error Error");
    });
 });
});

How can i get data (json format) from another domain?
My problem is: I want to get data from: http://pin-codes.in/api/pincode/400001/
I have tried to use CORS but it didn't work.
My console is:
GET http://pin-codes.in/api/pincode/400001 [HTTP/1.1 200 OK 780ms]
Error Error
My code by client-script:

$(document).ready(function() {
 $("#get_data_btn").click(function() {
   var data_path = "http://pin-codes.in/api/pincode/400001";
   $.getJSON(data_path, null)
    .done(function(data) {
      console.log("Success: " + data.status);
    })
    .fail(function(jqxhr, textStatus, error) {
      console.log("Error Error");
    });
 });
});
Share Improve this question asked Dec 11, 2013 at 4:11 user3089480user3089480 1011 silver badge11 bronze badges 1
  • possible duplicate of Ways to circumvent the same-origin policy – Felix Kling Commented Dec 11, 2013 at 4:30
Add a ment  | 

3 Answers 3

Reset to default 3

You probably don't own the other domain right?

No problem at all. Never mind nay-sayers, in puting everything is a yay!

Just use a simple proxy on your server or look into YQL.

this simple query will work:

select * from json where url="http://pin-codes.in/api/pincode/400001/ "

Just test this link (bypassing cross-domain security bull$#!7).
It will get the data you requested as normal plain json (no jsonp) data wrapped in callback-function cbfunc.

Have a look at this question for further info (I did quite a lot of yql scrape answers on SO).


Update:

Here is a crude fiddle demonstrating the whole process: so you enter a url, click fetch and watch the magic happen: http://jsfiddle/NbLYE/

function getJSON(url) {  //quick and dirty
  var script = document.createElement('script');
  script.setAttribute('src', url);
  script.setAttribute('type', 'text/javascript');
  document.getElementsByTagName('head')[0].appendChild(script);
}

function cbfunc(json){     //the callback function
   if(json.query.count){ 
      var data=json.query.results.json;
      // do your work here, like for example:
      //document.getElementById('output').innerHTML=data.toSource();
   } else {
      alert('Error: nothing found'); return false;
   }
}

function fetch(url){         //scrape the url you'd want
   var yql="select * " + 
           " from json" +
           " where url='" + url + "';";
   yql="http://query.yahooapis./v1/public/yql?q=" +
       encodeURIComponent(yql) +
       "&format=json" +
       "&callback=cbfunc";
   getJSON(yql);
}

That should get you started (and motivated that it is easy).

Hope this helps!

You don't have the correct CORS headers on your server.

You need to add

Access-Control-Allow-Origin: *

(or something similar) server-side to the response.

Edit: From the HTTP response, it appears you are using PHP. Use the header function in your response.

<?php header('Access-Control-Allow-Origin: *'); ?>

You can't do it using jquery only, you can use any server side script like PHP

Try using php,

<?php
    echo file_get_contents('http://pin-codes.in/api/pincode/400001');
?>

Save above code in pincode.php and use jquery like,

$(document).ready(function() {
    $("#get_data_btn").click(function() {
        var data_path = "pincode.php";
        $.getJSON(data_path, null)
            .done(function(data) {
                console.log("Success: " + data.status);
            })
            .fail(function(jqxhr, textStatus, error) {
                console.log("Error Error");
            });
    });
});

Also read this

发布评论

评论列表(0)

  1. 暂无评论