简体   繁体   English

jQuery-调用$ .getJSON时出错

[英]jQuery - Error calling $.getJSON

I am an absolute beginner to jQuery and json, and I am trying to parse a json object using jquery. 我是jQuery和json的绝对入门者,并且我尝试使用jquery解析json对象。 The json object is json对象是

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

The code I am using to parse the json object is 我用来解析json对象的代码是

$.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). 我尝试了几种修复方式(针对AJAX请求使用了不同的示例)。

----Edit---- Despite not accessing any cross-domain file, I took the suggestion from this question: ----编辑----尽管未访问任何跨域文件,但我还是从以下问题中得出了建议:

XMLHttpRequest cannot load an URL with jQuery XMLHttpRequest无法使用jQuery加载URL

And added "&callback=?" 并添加了“&callback =?” to my code url, which got rid of the error mentioned above but now gives another error 到我的代码url,摆脱了上面提到的错误,但现在又给出了另一个错误

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. 由于只有一行,因此它似乎是指向json对象中的冒号,这似乎非常奇怪。 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 PS:除了使dataType为“ jsonp”之外,我还采用了ajax方法(下面我曾尝试过)user1105704的建议,它再现了与意外令牌有关的错误

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). 我提供的URL是完整的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. 一旦将其切换到文件位置(从根目录开始,因此,如果文件位于文件夹a中,则位于a / get_reviews.php中),它就像一个超级按钮一样起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM