SyntaxError:检索JSON对象时无效的标签

时间:2012-08-09 11:30:12

标签: javascript jquery json

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

  1. SyntaxError:Mozilla中的无效标签。
  2. 未捕获的SyntaxError:意外的令牌:在Chrome中
  3. 我的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>
    

3 个答案:

答案 0 :(得分: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) {


            }

答案 1 :(得分:0)

嘿,你正在尝试使用

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

像这样使用

答案 2 :(得分:0)

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

我通过用方括号括起我的返回值来解决它:

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

然后“evaled”我的返回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) {
            }
        });