【发布时间】:2012-08-06 14:52:35
【问题描述】:
在检索 JSON 对象时,我收到以下错误:
- 语法错误:Mozilla 中的标签无效。
- Uncaught SyntaxError: Unexpected token : in Chrome
我的 JSON 对象是如下所示的格式:
{
"userName" : "clevermeal835",
"userRole" : "Participant",
"userAccountStatus" : "Active"
}
代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="Scripts/jquery-min.js"></script>
<script src="Scripts/jquery_validate.js"></script>
<script>
$(document).ready(function() {
loadLatestTweet();
});
function loadLatestTweet(){
var xhr = new XMLHttpRequest();
var uid = "clevermeal835";
var pwd = "Welcome_1";
var userType = "participant";
var surl = 'http://localhost:8080/RESTlet_WS/MobiSignIn/{"userName":"'+uid+'","password":"'+pwd+ '","userType":"'+userType+'"}&callback=?';
var jqxhr = $.getJSON(surl, function() {
alert("success");
}).success(function() { alert("second success"); }).error(function() { alert("error"); }).complete(function() { alert("complete"); });
jqxhr.complete(function(){ alert("second complete"); });
}
</script>
</head>
<body>
<input id="jsonpbtn2" type="submit" value="button" />
</body>
</html>
【问题讨论】:
-
您告诉 jQuery 期待 JSONP,但响应看起来像 JSON,而不是 JSONP。响应被评估为 JavaScript,但由于 JSON 不是有效的 JavaScript,您会收到该错误。您必须设置服务器以返回 JSONP。
-
您能否通过设置 server 返回 jsonp 来澄清您的意思。其实json、jsonp我都试过了。
-
答案在我链接到的问题中:stackoverflow.com/a/2816696/218196。我的意思是您必须配置/编程您的服务器以返回 JSONP,而不是(仅)JSON。如果您不知道 JSONP 是什么,请查看这篇 Wikipedia 文章:en.wikipedia.org/wiki/JSONP。
-
我只需要 JSON 对象而不需要 JSONP。请您清除它。
标签: javascript jquery json