Access JSON service from localhost or file: //

I am creating an html page designed to run locally on a PC, preferably without starting a local server (file: //). I also use jQuery to make / AJAX manipulation a little easier.

I am trying to download 2 results from twitter API, but I am getting error message. 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 did not change the result.

$.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 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 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?

+5
source share
3 answers

- script , , .

+11

JQuery JSONP URL

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

Do not use this. $.getJSON() This is not flexible .. You can use

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

it can easily be accessed using html coding ....

-2
source

All Articles