【发布时间】:2019-09-26 08:37:57
【问题描述】:
我最近正在使用 adobe InDesign 扩展,我想使用 jquery ajax POST 调用将 xml 文件上传到我的服务器,因此,我必须从文件系统中读取 XML 文件并将其存储到变量中然后将该变量作为正文传递到发布请求中,这是我的代码
function uploadDocument( onSuccess, onError, onComplete) {
var token = localStorage.getItem("token");
writeLogs("uploadDocument function \n " + token );
var result = window.cep.fs.readFile("/Users/mac41589/Downloads/test-xmls/post.xml");
var xmlStr = "";
if(result.err == 0){
writeLogs("file read complete " + ' ' + result.data)
xmlStr = result.data;
alert("type of xmlStr new" + ' ' + typeof(xmlStr));
$.ajax({
url : "https://xyz.abc.com/capi-demo/article?customerNumber=888",
method: "POST",
data: xmlStr,
beforeSend : function(xhr) {
xhr.setRequestHeader("Authorization", "Bearer " + token);
xhr.setRequestHeader("Content-Type", "application/xml");
},
complete: function(xhr) {
alert("on complete with code" + ' ' + xhr.status + ' ' + xhr.statusText );
//onComplete();
},
success : function(response) {
alert("file upload success with response : " + ' ' +response);
},
error : function(jqXHR, textStatus, errorThrown) {
alert('file upload fail with error -- ' + jqXHR.status + ' textStatus: ' + textStatus + ' errorThrown: ' + errorThrown);
}
});
}
}
这里正是我要发送的 XML 文件:
<document xmlns="http://pubpress.com/document">
<properties>
<magazineNumber>95100</magazineNumber>
</properties>
<article>
<pam:message xmlns:pam="http://xax.org/namespaces/pam/2.0/" xmlns:ppc="http://axa.com/content" xml:base="/article/content/39992.xml">
<pam:article>
<head xmlns="http://www.w3.org/1999/xhtml">
<dc:identifier xmlns:dc="http://purl.org/dc/elements/1.1/">888-create.xml</dc:identifier>
<pam:status/>
</head>
<body xmlns="http://www.w3.org/1999/xhtml"><p>Sample body text</p></body>
</pam:article>
</pam:message>
</article>
</document>
因此,每当我执行此 POST 调用时,它都会返回 404 错误 Not Found 但是当我发送错误(不希望到服务器)的 XML 文件时,它会显示 400 错误请求。 错误的 xml(服务器不需要)如下:
<?xml version="1.0" encoding="UTF-8"?>
<variable type="NameValuePair[]">
<item type="NameValuePair">
<name type="String"><![CDATA[No Data Found]]></name>
<value type="String"><![CDATA[95990070]]></value>
</item>
</variable>
我无法找到为什么这个 POST 调用从 ajax 调用返回 404,而在 PostMan 中具有相同参数的相同调用运行良好。 先感谢您.. 对此的任何帮助将不胜感激。
【问题讨论】:
-
404 确实意味着您发布到了错误的网址,请检查您提交的网址
-
我在邮递员中使用了相同的网址,效果很好
-
您能否通过检查网络服务器的访问日志来验证 url 没有在其他地方被修改?
-
@PrasadPawar 您可以从 Postman 客户端获取代码并对其进行测试
标签: javascript jquery ajax xml adobe-indesign