【问题标题】:How to solve JSON Syntax error. How to get response?如何解决 JSON 语法错误。如何得到回应?
【发布时间】:2017-02-26 08:19:08
【问题描述】:

这是 UPS 运输网络服务 API。我收到此错误:

{"Error":{"Code":"4","Description":"JSON 语法错误"}}

AJAX 代码:

  <script>          
        var formData = { "UPSSecurity": { "UsernameToken": { "Username":"xxx.com", "Password":"xxxxx" }, "ServiceAccessToken": { "AccessLicenseNumber":"1234D567DF67" } }, "RateRequest": { "Request": { "RequestOption":"Rate", "TransactionReference": { "CustomerContext":"Test saran" } }, "Shipment": { "Shipper": { "Name":"Saravanan", "ShipperNumber":"Y72A41", "Address": { "AddressLine":["2311 York Rd"], "City":"Sebastopol", "StateProvinceCode":"CA", "PostalCode":"95473", "CountryCode":"US" } }, "ShipTo": { "Name":"ShipToName", "Address": { "AddressLine":["195 N main st"], "City":"Sebastopol", "StateProvinceCode":"CA", "PostalCode":"95472", "CountryCode":"US" } }, "ShipFrom": { "Name":"ShipFromName", "Address": { "AddressLine":"2311 York Rd", "City":"Sebastopol","StateProvinceCode":"CA", "PostalCode":"95473", "CountryCode":"US" } }, "Service": { "Code":"03", "Description":"Express" }, "Package": { "PackagingType": { "Code": "02", "Description": "Rate" }, "Dimensions": { "UnitOfMeasurement": { "Code": "IN", "Description": "inches" }, "Length": "7", "Width": "5", "Height": "2" }, "PackageWeight": { "UnitOfMeasurement": { "Code": "Lbs", "Description": "pounds" }, "Weight": "10" } }, "ShipmentRatingOptions": { "NegotiatedRatesIndicator": "" } } } };

        $.ajax({
            type       : "POST",
            url        : "https://wwwcie.ups.com/rest/Rate",
            crossDomain: true,
            timeout    : 240000,
            data       : formData,
            dataType   : 'json',
            success    : function(response) 
            {
                alert("result="+response);
            }  
        }); 

    </script>

我不知道我错在哪里。但是这个请求可以通过“POST MAN”和“ARC”工具完美运行。 请更新答案。

【问题讨论】:

    标签: php json ups


    【解决方案1】:

    您没有发送 JSON;当你给它一个对象时,jQuery 的默认序列化是 URI 编码。要改为发送 JSON,请将您的对象转换为带有 JSON.stringify 的字符串。您还应该使用contentType 确定您发送的是 JSON,所以:

    contentType: 'application/json',
    data       : JSON.stringify(formData),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多