这是UPS发货的webservices API。我收到了这个错误:
{“错误”:{“代码”:“4”,“描述”:“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”工具完美运行。 请更新答案。
答案 0 :(得分:4)
你没有发送JSON;当你给它一个对象时,jQuery的默认序列化是URI编码。要发送JSON,请将对象转换为JSON.stringify
的字符串。您还应该使用contentType
确定您发送的内容是JSON,所以:
contentType: 'application/json',
data : JSON.stringify(formData),