5

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?

dtech
  • 13,741
  • 11
  • 48
  • 73

3 Answers3

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:

    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.

no.good.at.coding
  • 20,221
  • 2
  • 60
  • 51
  • Seems reasonable but now I get a different error: "Uncaught TypeError: Cannot read property 'length' of undefined" – dtech Mar 14 '11 at 20:07
  • It seems like you're trying to access the `length` property on what you think is an array but is actually `undefined`. Ensure that your data actually exists (`if(!assumedArray){//handle missing data}else{//do usual processing}`. What does Twitter return when there are no results for a search query? Also, take a look at http://stackoverflow.com/questions/3040069/uncaught-typeerror-cannot-read-property-length-of-undefined-jquery for some suggestions on how to debug this (and other) exceptions. – no.good.at.coding Mar 14 '11 at 20:37
  • 1
    Here's a [bare-bones fiddle](http://jsfiddle.net/nogoodatcoding/reJN6/4/) that fetches tweets for a particular user and also displays results of a search. It that of any help? – no.good.at.coding Mar 14 '11 at 20:47
  • I was just using the built-in jQuery functions and didn't access length myself at all. But nevermind, I found the solution to my asking problem, thx :) – dtech Mar 14 '11 at 21:03
6

Add the JQuery's JSONP callback to the URL

$.getJSON("http://search.twitter.com/search.json?callback=?", {
epascarello
  • 204,599
  • 20
  • 195
  • 236
-2

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

Abi
  • 65
  • 1
  • 6