尝试使用JQuery Ajax()发送JSON时出现Parse错误

时间:2011-04-06 21:12:16

标签: jquery ajax

我正在尝试将以下json发送到Web方法。 Firebug报告以下错误:

  

XML解析错误:找不到元素位置:moz-nullprincipal:{d9dc6bef-4ec9-4899-b8df-7319db0e47cf}第1行,第1列:

我正在编辑请求标头以包含内容类型。有人可以帮帮我吗?

  var request = {"uid":"Test","application":",myApplication","localization":"en-us","id":"aae49326","context":"","clientToken":"puttokenhere2","version":"2.0.87","timestamp":"4/6/2011 13:15:57","status":"COMPLETED","data":""};
        jQuery.ajax({ beforeSend: function(xhrObj) {
            xhrObj.setRequestHeader("Method", "POST");
            xhrObj.setRequestHeader("Content-Type", "application/json; charset=\"utf-8\";");
        },            
            async: false,
            type: "POST",
            contentType: "application/json; charset=\"utf-8\";",
            dataType: "json",
            url: MILESTONEURL,
            data: request,
            complete: function(response, status) {
                if ("success" == status) {
                    alert("SUCCESS:\n" + response.responseText);
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                var response = eval("(" + XMLHttpRequest.responseText + ')'); ;
                alert("FAILED:\n" + XMLHttpRequest.responseText);
            }
        });
 }

问题发生在网络服务上 - 关闭

1 个答案:

答案 0 :(得分:0)

第1行

键不应该有双引号

试试这个:

   var request = {uid:"Test",application:",myApplication",localization:"en-us",id:"aae49326",context:"",clientToken:"puttokenhere2",version:"2.0.87",timestamp:"4/6/2011 13:15:57",status:"COMPLETED",data:""};
    jQuery.ajax({ beforeSend: function(xhrObj) {
        xhrObj.setRequestHeader("Method", "POST");
        xhrObj.setRequestHeader("Content-Type", "application/json; charset=\"utf-8\";");
    },            
        async: false,
        type: "POST",
        contentType: "application/json; charset=\"utf-8\";",
        dataType: "json",
        url: MILESTONEURL,
        data: request,
        complete: function(response, status) {
            if ("success" == status) {
                alert("SUCCESS:\n" + response.responseText);
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            var response = eval("(" + XMLHttpRequest.responseText + ')'); ;
            alert("FAILED:\n" + XMLHttpRequest.responseText);
        }
    });
 }