6

检索 JSON 对象时,我收到以下错误:

  1. SyntaxError:Mozilla 中的标签无效。
  2. 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>
4

3 回答 3

3

你可以让代码看起来像这样

var params = { "Username": UserNameValue,"Password": PassValue};


        $.ajax({
            type: "POST",
            url: "http://localhost:8080/RESTlet_WS/MobiSignIn/",
            contentType: 'application/json',

            data: JSON.stringify(params),
            dataType: 'json',
            async: false,
            cache: false,
            success: function (response) {


            },
            error: function (ErrorResponse) {


            }
于 2013-01-29T04:52:31.450 回答
0

嘿,试试这个你正在使用的网址

var params = encodeURIComponent('{"userName":"'+uid+'","password":"'+pwd+ '","userType":"'+userType+'"}');
var surl = 'http://localhost:8080/RESTlet_WS/MobiSignIn?params='+params+'&callback='

像这样使用

于 2013-02-28T09:01:22.470 回答
0

调用 asmx Web 服务 (.NET) 时,我遇到了完全相同的问题。

我通过用方括号括起来我的返回值来解决它,如下所示:

return @"[ {{ ""status"" : ""OK"", ""message"" : ""Success !!!"" }} ]";

然后因为讨厌的 d 参数而“评估”了我的 return var:

$.ajax({
            type: "POST",
            url: 'http://www.example.com/',
            contentType: 'application/json; charset=utf-8',
            data: '{name: "' + vName + '", target: "' + vTarget + '", phone: "' + vPhone + '", timeframe: ' + true + '}',
            dataType: 'json',
            success: function (msg) {

                jsonMsg = eval(msg.d);

                alert(jsonMsg.status);
                alert(jsonMsg.message);
            },
            error: function (xhr, msg) {
            }
        });
于 2013-11-19T14:22:27.700 回答