【问题标题】:XMLHTTP Post not workingXMLHTTP Post 不工作
【发布时间】:2017-07-27 20:20:13
【问题描述】:

我正在尝试使用 XMLHTTP 获取 PDF 文件以进行响应,并使用 XMLHTTP get 发布响应。 Get 部分运行良好,但 Post 部分没有得到响应。

   var Req = new XMLHttpRequest();
Req.open("POST",'http://192.168.56.103/API/Twebservice.asmx/Updatepdf', false);
Req.onload = function (oEvent) {
  // Uploaded.


var blob = function(){var xhr = new XMLHttpRequest()
 xhr.open("GET", "http://www.pdf995.com/samples/pdf.pdf",true);
 xhr.send();

 if (xhr.status === 200) { 
 var test=xhr.responseText;//console.log(test)

 }} }
//GetPDF();
Req.send(blob());

希望有人能提供帮助。

【问题讨论】:

  • Req.send(blob()); 没有意义。你实际上在做Req.send(undefined); 你认为 blob() 在做什么?
  • 这只是我尝试,但我认为由于异步性质......我无法将响应文本放入变量中
  • 所以您在取回数据后进行调用,但您在代码中将面临的问题是同源策略
  • 我可以访问两台服务器...PDF 文件的 URL 仅用于测试。我无法将数据放入响应中。在没有任何数据的情况下触发 post callgets。
  • 您将异步调用视为同步调用。 blob 调用需要一个 onload,在 onload 中你调用另一个 Ajax 调用。

标签: javascript post get xmlhttprequest arraybuffer


【解决方案1】:

将调用视为异步。在第一个完成后调用第二个。

function firstCall() {
  var xhr = new XMLHttpRequest()
  xhr.open("GET", "path1", true); 
  xhr.onload = function () {
    secondCall(xhr.responseText);
  };
  xhr.onerror = function () {
      console.error("Error", xhr.statusText);
  };
  xhr.send();
}

function secondCall(data) {
  var xhr = new XMLHttpRequest()
  xhr.open("POST", "path2", true);
  xhr.onload = function () {
    console.log("done");    
  };
  xhr.onerror = function () {
      console.error("Error", xhr.statusText);
  };
  xhr.send(data);
}

【讨论】:

  • 感谢 Epascarello ...修复了它。现在我知道我哪里出错了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-03
  • 2015-11-21
  • 2014-05-07
相关资源
最近更新 更多