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

javascript - Accessing JSON service from localhost or file: - Stack Overflow

programmeradmin2浏览0评论

I am making a html page intended to be run locally on a PC, preferably without a local server runing (file://). I am also using jQuery to make manipulation/AJAX a little easier.

I am trying to load 2 results from the twitter API, but I get an error. The code is as follows:

$.getJSON(".json?screen_name=someuser&count=9", {},
    function (data) {
        $.each(data.items, doSomething1);
    });
$.getJSON(".json?q=somequery&result_type=recent&count=9", {},
    function (data) {
        $.each(data.items, doSomething2);
    });

I also tried the following code, but it didn't change the outcome.

$.getJSON(".json",
    {
        count:          "9",
        screen_name:    "someuser"
    },
    function(data) {
        $.each(data.items, updateAWTweets);
    });
$.getJSON(".json",
    {
        q:              "somequery",
        result_type:    "recent",
        count:          "9"
    },
    function(data) {
        $.each(data.items, updateHashTagTweets);
    });

I get the following error in chrome (on a localhost server):

XMLHttpRequest cannot load .json?q=somequery&result_type=recent&count=9. Origin http://localhost:62153 is not allowed by Access-Control-Allow-Origin.

or (with a file:// link)

XMLHttpRequest cannot load .json?screen_name=someuser&count=9. Origin null is not allowed by Access-Control-Allow-Origin.

Does anyone know how I can fix this?

I am making a html page intended to be run locally on a PC, preferably without a local server runing (file://). I am also using jQuery to make manipulation/AJAX a little easier.

I am trying to load 2 results from the twitter API, but I get an error. The code is as follows:

$.getJSON("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=someuser&count=9", {},
    function (data) {
        $.each(data.items, doSomething1);
    });
$.getJSON("http://search.twitter.com/search.json?q=somequery&result_type=recent&count=9", {},
    function (data) {
        $.each(data.items, doSomething2);
    });

I also tried the following code, but it didn't change the outcome.

$.getJSON("http://api.twitter.com/1/statuses/user_timeline.json",
    {
        count:          "9",
        screen_name:    "someuser"
    },
    function(data) {
        $.each(data.items, updateAWTweets);
    });
$.getJSON("http://search.twitter.com/search.json",
    {
        q:              "somequery",
        result_type:    "recent",
        count:          "9"
    },
    function(data) {
        $.each(data.items, updateHashTagTweets);
    });

I get the following error in chrome (on a localhost server):

XMLHttpRequest cannot load http://search.twitter.com/search.json?q=somequery&result_type=recent&count=9. Origin http://localhost:62153 is not allowed by Access-Control-Allow-Origin.

or (with a file:// link)

XMLHttpRequest cannot load http://api.twitter.com/1/statuses/user_timeline.json?screen_name=someuser&count=9. Origin null is not allowed by Access-Control-Allow-Origin.

Does anyone know how I can fix this?

Share Improve this question asked Mar 14, 2011 at 14:07 dtechdtech 14.1k11 gold badges52 silver badges73 bronze badges 2
  • That error is a cross-site scripting warning. – Mike Atlas Commented Mar 14, 2011 at 14:10
  • possible duplicate of jQuery.getJSON not working for twitter – epascarello Commented Mar 14, 2011 at 14:31
Add a comment  | 

3 Answers 3

Reset to default 11

You're running into the same-origin policy restriction - your script can't access any other domain apart from the one it was loaded from.

  1. You could give JSONP a try - that's one common solution to getting data across domains:

    • http://www.chazzuka.com/blog/?p=221
    • http://jquery-howto.blogspot.com/2009/04/twitter-jsonjsonp-api-url.html

    Your code would look something like this (note the addition of callback=? to the URL):

    $.getJSON("http://search.twitter.com/search.json?q=somequery&result_type=recent&count=9&callback=?", 
              {},
              function (data) {
                      $.each(data.items, doSomething2);  
         });
    
  2. Another option is to setup a proxy - you can use Apache httpd as a proxy/reverse proxy to get around this restriction.

Add the JQuery's JSONP callback to the URL

$.getJSON("http://search.twitter.com/search.json?callback=?", {

Don't use this $.getJSON() Its not flexible.. You can use

$.ajax({
    url:"test.json",
    dataTypr:"json",
    async:false
}).responseText;

this can easily accessed by the html coding....

发布评论

评论列表(0)

  1. 暂无评论