【问题标题】:404 in JQuery Ajax POST call while sending XML to server将 XML 发送到服务器时,JQuery Ajax POST 调用中的 404
【发布时间】: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


【解决方案1】:

除了确保 url 接受发布的 xml 之外,您还应该将 contentType: "text/xml", 的 ajax 选项添加到您的配置中。

在这里,我使用myApp 将东西从全局范围中取出(不是问题的一部分,但对我来说,这样做在实践中会更好)。我再次重构为.ajax() 的承诺形式,因为我更喜欢它,然后我可以用一些名称间隔的错误处理程序替换那里的函数,例如我所有的ajax。(那些传递的东西是什么,回调?)

我还在那里看到了一些错误,例如typeof()。这假设 ES6。

// create a namespace object
var myApp = myApp || {
  url: "https://xyz.abc.com/capi-demo/article?customerNumber=888"
};
// borrow some code from https://stackoverflow.com/a/23861977/125981
myApp.isXHR = function(maybe) {
  if (!maybe) {
    return false;
  }
  if (!('readyState' in maybe)) {
    return false;
  }
  if (!('responseText' in maybe)) {
    return false;
  }
  if (!('status' in maybe)) {
    return false;
  }
  if (!('statusText' in maybe)) {
    return false;
  }
  return true;
};

myApp.writeLogs = function(logmessage) {
  // just to act as placeholder for the answer
};

myApp.processMessage = function(message, silent = true) {
  if (silent) {
    alert(message);
  } else { // or
    console.log(message);
    // or something else like send to server via ajax to log or some such
  }
};

myApp.getDocumentContent = function() {

  let result = window.cep.fs.readFile("/Users/mac41589/Downloads/test-xmls/post.xml");
  //var xmlStr = "";
  let getResult = {
    xmlStr: "",
    hasContent: false,
    error: result.err
  };
  if (result.err == 0) {
    myApp.writeLogs("file read complete " + ' ' + result.data)
    myApp.processMessage("type of xmlStr new" + ' ' + (typeof result.data));
    getResult.xmlStr = result.data;
    getResult.hasContent = true;
  }
  return getResult;
};

myApp.sendContent = function(contentObj) {
  let token = localStorage.getItem("token");
  myApp.writeLogs("uploadDocument function \n " + token);
  myApp.writeLogs("file read complete " + contentObj.xmlStr);
  myApp.processMessage("type of xmlStr new " + (typeof contentObj.xmlStr));

  $.ajax({
      url: myApp.url,
      method: "POST",
      data: contentObj.xmlStr,
      contentType: "text/xml",
      beforeSend: function(xhr, settings) {
        xhr.setRequestHeader("Authorization", "Bearer " + token);
        xhr.setRequestHeader("Content-Type", "application/xml");
      }
    })
    .always(function(dataOrJqXHR, textStatus, jqXHROrErrorThrown) {
      // determine which parameter is which when the .always() is called
      let my_jqXHR = null;
      let data = null;
      let errorThrown = null;
      if (myApp.isXHR(dataOrJqXHR)) {
        my_jqXHR = dataOrJqXHR;
        errorThrown = jqXHROrErrorThrown;
      }
      if (myApp.isXHR(jqXHROrErrorThrown)) {
        my_jqXHR = jqXHROrErrorThrown;
        data = dataOrJqXHR;
      }
      let status = my_jqXHR.status;
      // do something with status
      myApp.processMessage("on complete with code" + ' ' + status + ' ' + errorThrown);
    })
    .done(function(data, textStatus, jqXHR) {
      myApp.processMessage("file upload success with response : " + ' ' + textStatus + ' ' + data);
    })
    .fail(function(jqXHR, textStatus, errorThrown) {
      myApp.processMessage('file upload fail with error -- ' + jqXHR.status + ' textStatus: ' + textStatus + ' errorThrown: ' + errorThrown);
    })
    .then(function(data, textStatus, jqXHR) {
      myApp.processMessage("file upload success with response : " + ' ' + textStatus + ' ' + data);
    }, function(jqXHR, textStatus, errorThrown) {
      myApp.processMessage('file upload fail with error -- ' + jqXHR.status + ' textStatus: ' + textStatus + ' errorThrown: ' + errorThrown);
    });
};

myApp.uploadDocument = function(onSuccess, onError, onComplete) {
  let contentObj = myApp.getDocumentContent();

  if (contentObj.hasContent && contentObj.err == 0) {
    myApp.sendContent(contentObj);
  } else {
    myApp.processMessage("No Content" + contentObj.err);
  }
};
// call it, not sure what these passed thing are, and seem unused
myApp.uploadDocument(onSuccess, onError, onComplete);
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;

【讨论】:

  • 更改 contentType: "text/xml" 后再次响应 404 not found
  • 我添加了更多内容,因此您可能会获得有关错误的更好信息
猜你喜欢
  • 2012-12-29
  • 1970-01-01
  • 2015-06-11
  • 2018-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多