使用jquery ajax调用无效的json跟踪

时间:2015-02-07 09:34:44

标签: jquery ajax json

    $.ajax({
        type: 'POST',
        headers: { "cache-control": "no-cache" },
        url: '/Service.asmx/ProcessRequest',
        async: true,
        cache: false,
        data: 'controller=cart&add=1&ajax=true&qty=' + ((quantity && quantity != null) ? quantity : '1') + '&id_product=' + idProduct + '&token=' + static_token + ((parseInt(idCombination) && idCombination != null) ? '&ipa=' + parseInt(idCombination) : ''),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (jsonData, textStatus, jqXHR) {
        }
         error: function (XMLHttpRequest, textStatus, errorThrown) {
          alert(XMLHttpRequest.responseText);
         }

服务器功能:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string ProcessRequest(string controller, int add, bool ajax, int qty, int id_product, string token, string ipa)
{
    string results = "{" + CartControllerResponse() + "}";
    return results;
}

我收到这样的ajax错误:{"Message":"Invalid JSON primitive: controller.","StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您发送的数据是无效的JSON。应采用以下格式:

{"controller":"cart","add":1, "ajax":true ...}

但是你以这种格式发送它:

 data: 'controller=cart&add=1&ajax=true ...