0

I am an absolute beginner to jQuery and json, and I am trying to parse a json object using jquery. The json object is

{"interested":1,"like":3,"dislike":2}

The code I am using to parse the json object is

$.getJSON( "get_reviews.php?mid=x&uid=0",
function (data)
{
    console.log("parsing");
    Interested = data.interested;
    Dislike = data.dislike;
    Like = data.like;
    }

The error I'm getting is

XMLHttpRequest cannot load http://www.********.***/get_reviews.php?mid=x&uid=0. Origin http://********.*** is not allowed by Access-Control-Allow-Origin.

Anyone knows where I'm going wrong? I've tried fixing it in several ways (using different examples for AJAX requests).

----Edit---- Despite not accessing any cross-domain file, I took the suggestion from this question:

XMLHttpRequest cannot load an URL with jQuery

And added "&callback=?" to my code url, which got rid of the error mentioned above but now gives another error

Uncaught SyntaxError: Unexpected token :

Since there's only one line, it seems to refer to the colon in the json object, which seems extremely odd. Any idea why this might be happening.

PS: I also took user1105704's suggestion below with the ajax method (I'd tried this before) except making the dataType "jsonp", and it reproduces the error regarding the Unexpected token

Community
  • 1
  • 1
Raveesh Bhalla
  • 921
  • 8
  • 19
  • 1
    http://stackoverflow.com/q/1653308/1095276 ,you are trying to make a crossdomain request – kvc Dec 24 '11 at 16:16
  • It's not a cross-domain request. Sorry for not making it clearer. – Raveesh Bhalla Dec 24 '11 at 16:18
  • Maybe it's a header issue. Check out http://stackoverflow.com/questions/3595515/xmlhttprequest-error-origin-null-is-not-allowed-by-access-control-allow-origin and http://stackoverflow.com/questions/1653308/access-control-allow-origin-multiple-origin-domains. – Ayman Safadi Dec 24 '11 at 16:55
  • can you please share the response headers you see in firebug ? – Diode Dec 24 '11 at 17:04
  • If you change to JSONP, you need to also change the response. JSONP returns a JS Script, not JSON. Your response should be `func({"interested":1,"like":3,"dislike":2})`. Where `func` is the value of `callback`. – gen_Eric Dec 24 '11 at 21:52

1 Answers1

0

looks like I was making a much smaller error than I realised. The URL I gave was a complete URL (http://www.*.*/get_reviews.php).

It turns out that's the reason it was being considered a cross-domain access. As soon as I switched it to a file location (location from root, so if file is in folder a, then a/get_reviews.php), it worked like a charm.

Raveesh Bhalla
  • 921
  • 8
  • 19