如何解决JSON语法错误.如何获得回应? [英] How to solve JSON Syntax error. How to get response?
问题描述
这是UPS运送Web服务API.我收到此错误:
This is the UPS shipping webservices API. I am getting this error :
{错误":{代码":"4",描述":"JSON语法错误"}}
{"Error":{"Code":"4","Description":"JSON Syntax error"}}
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"工具可以完美运行. 请更新答案.
I don't know where I am wrong. But this request is running perfectly by "POST MAN" and "ARC" tools. Please update answer.
推荐答案
您不发送JSON;当给jQuery对象一个对象时,它的默认序列化是URI编码.要发送JSON,请使用JSON.stringify
将您的对象转换为字符串.您还应该使用contentType
确定发送的是JSON,所以:
You're not sending JSON; jQuery's default serialization when you give it an object is URI-encoding. To send JSON instead, convert your object to a string with JSON.stringify
. You should also identify that what you're sending is JSON using contentType
, so:
contentType: 'application/json',
data : JSON.stringify(formData),
这篇关于如何解决JSON语法错误.如何获得回应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!