如何使用XmlHttpRequest为网站发送参数到Sever(云功能)

时间:2018-05-10 03:55:58

标签: javascript node.js xmlhttprequest bootstrap-4

我正在尝试使用XMLHttpRequest调用我的服务器。目前,我在localhost中运行我的网站。所以我写了这样的陈述:

var xhttp = new XMLHttpRequest();
  var url = "https://us-central1-deyapay-192704.cloudfunctions.net/Add"; 
  var params1 = {
  "AuthID": userid,
  "Amount":amt};
  var params = JSON.stringify(params1);
  console.log(amt);
  console.log(userid);
  xhttp.open("POST", url, true);
  xhttp.setRequestHeader( 'Access-Control-Allow-Origin', '*');
  //xhttp.withCredentials = false;
  xhttp.setRequestHeader("Content-type", "application/json");

  xhttp.onload = function() {
    var data = JSON.parse(this.responseText);
     console.log(data);
  }
  xhttp.onreadystatechange = function() {//Call a function when the state changes.
    if(xhttp.readyState == 4 && xhttp.status == 200) {
        alert(xhttp.responseText);
    }
}
xhttp.send(
    params
  );

因此,每当我运行该HTML文件时,我都会收到“Failed to load resource: the server responded with a status of 500 ()""Failed to load https://us-central1-deyapay-192704.cloudfunctions.net/Add: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:5000' is therefore not allowed access. The response had HTTP status code 500."等错误,并且在发送参数时会显示错误。截图为: enter image description here

enter image description here 在服务器上,我收到错误,如“Argument”documentPath“不是有效的ResourcePath。路径必须是非空字符串。”从XMLHttpRequest传递到sever的值为null。在服务器端,它打印NAN。 如何解决这个错误?

0 个答案:

没有答案