我使用以下代码连接到供应商。运行时,我将返回状态代码0.使用Wireshark进行评估时,我发现请求因以下原因而失败:
错误:传入邮件的意外邮件格式为“原始”。该操作的预期消息格式为“Xml”,“Json”。
我从文档中看到dataType设置仅定义传入数据。如何将输出设置为JSON?
(请原谅原始编码,此刻只是一个shell)
var key = "xxxxxx";
$.ajax({
type : "POST",
dataType : "JSON",
url : "http://url",
data : {
"CityName":cty,
"FirmOrRecipient":name,
"LicenseKey":key,
"PrimaryAddressLine":s1,
"SecondaryAddressLine":s2,
"State":st,
"ZipCode":zip
},
success : function (data, status, xhr) {
var pretty = JSON.stringify(data, null, 4).replace( / /g, ' ').replace( /\n/g, '<br />');
$('div#results').html(pretty);
},
error : function(jqXHR, textStatus, errorThrown){
if (jqXHR.status === 0) {
alert('ERROR: \n Failed to connect to PAV.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('ERROR: \n Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('ERROR: \n Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('ERROR: \n Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('ERROR: \n Time out error.');
} else if (exception === 'abort') {
alert('ERROR: \n Ajax request aborted.');
} else {
alert('ERROR: \n Uncaught Error.\n' + jqXHR.responseText);
}
},
});
答案 0 :(得分:0)
尝试设置内容类型。
$.ajax({
type : "POST",
dataType : "JSON",
contentType: 'application/json',
我还要确保您发送有效的json数据(jsonlint.com)。
答案 1 :(得分:0)
您应该返回JSON格式的输出
如果您使用的是spring框架,则可以在返回类型之前注释该方法
@ResponseBody
因此spring会将其作为JSON格式返回。
但是如果你正在使用任何其他框架。请使用json jar文件并尝试构造JSON输出格式并返回相同的内容。只有这样你的ajax模块才能读取和处理