XMLHttpRequest在升级到core 7后未发送

huangapple go评论67阅读模式
英文:

XMLHttpRequest not sending after upgrade to core 7

问题

I am updating a .NET 4.8 application to a Core 7 application, and am having an issue with the XMLHttpRequest sending null to the server.

var url = 'ExportToExcel';
var fileName = filenameString + '.xlsx';
var req = new XMLHttpRequest();
var params = '{"SentData": ' + JSON.stringify(dataPacket) + '}';
req.open('POST', url, true);
req.setRequestHeader('Content-type', 'application/json; charset=utf-8');

req.responseType = 'blob';
req.onreadystatechange = function () {

  if (req.readyState == 4 && req.status == 200) {
    // Convert the Byte Data to BLOB object.
    var blob = new Blob([req.response], { type: 'application/octetstream' });

    // Check the Browser type and download the File.
    var isIE = false || !!document.documentMode;
    if (isIE) {
      window.navigator.msSaveBlob(blob, fileName);
    } else {
      var url = window.URL || window.webkitURL;
      link = url.createObjectURL(blob);
      var a = document.createElement('a');
      a.setAttribute('download', fileName);
      a.setAttribute('href', link);
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
    }
  }
};

req.send(params);
public ActionResult ExportToExcel(string SentData) // SentData is null
{

The server receives the request, but the data is null.

What changed when upgrading to Core?

英文:

I am updating a .NET 4.8 application to a Core 7 application, and am having an issue with the XMLHttpRequest sending null to the server.

var url = 'ExportToExcel';
var fileName = filenameString + '.xlsx';
var req = new XMLHttpRequest();
var params = '{"SentData": \'' + JSON.stringify(dataPacket) + '\'}';
req.open('POST', url, true);
req.setRequestHeader('Content-type', 'application/json; charset=utf-8');

req.responseType = 'blob';
req.onreadystatechange = function () {

  if (req.readyState == 4 && req.status == 200) {
    //Convert the Byte Data to BLOB object.
    var blob = new Blob([req.response], { type: 'application/octetstream' });

    //Check the Browser type and download the File.
    var isIE = false || !!document.documentMode;
    if (isIE) {
      window.navigator.msSaveBlob(blob, fileName);
    } else {
      var url = window.URL || window.webkitURL;
      link = url.createObjectURL(blob);
      var a = document.createElement('a');
      a.setAttribute('download', fileName);
      a.setAttribute('href', link);
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
    }
  }
};

req.send(params);

public ActionResult ExportToExcel(string SentData) // SentData is null
{ 

The server receives the request, but the data is null.

What changed when upgrading to Core?

答案1

得分: 1

服务器接收了请求,但数据为空。在升级到Core时有何变化?

xhttp.send()的参数是请求的主体,而不是请求的参数。因此,如果后端需要参数,脚本不会发送任何参数。

所以你有两个选择,要么将你的HTTP参数作为请求主体发送,要么作为查询字符串发送。

要作为查询字符串发送,你可以进行以下修改:

function httpPost(){

    var dataPacket = [];
    for (let i = 1; i <= 10; i++) {
        var items = {
            CustomerCode: "CustomerCode:" + i,
            RegNo: "RegNo:" + i,
            FleetNo: "FleetNo:" + i,

        }
        dataPacket.push(items); // 创建演示数据

    }

    var params = JSON.stringify(dataPacket);
    console.log("param", params)
    var url = 'http://localhost:5094/Home/ExportToExcel?SentData=' + params + ''; // 将参数作为查询字符串传递

    console.log("url", url)


    var req = new XMLHttpRequest();
    req.open('POST', url, true);
    req.setRequestHeader('Content-type', 'application/json; charset=utf-8');
    req.send();
}


var response = httpPost();

输出:

XMLHttpRequest在升级到core 7后未发送

XMLHttpRequest在升级到core 7后未发送

XMLHttpRequest在升级到core 7后未发送

注意: 请参考官方文档以获取更多详细信息。并且了解asp.net core FromBody

英文:

> The server receives the request, but the data is null. What changed
> when upgrading to Core?

The xhttp.send()'s parameter is the request's body, not request's parameter. So if the backend need parameters, the script does not send any parameters.

So you have two choices here, either send your http parameter as request body or as query string.

For sending as query string you can modify as following:

<!-- language: c# -->
function httpPost(){

            var dataPacket = [];
            for (let i = 1; i &lt;= 10; i++) {
                var items = {
                    CustomerCode: &quot;CustomerCode:&quot; + i,
                    RegNo: &quot;RegNo:&quot; + i,
                    FleetNo: &quot;FleetNo:&quot; + i,

                }
                dataPacket.push(items); // Creating Demo Data 

            }

            var params = JSON.stringify(dataPacket);
            console.log(&quot;param&quot;,params)
            var url = &#39;http://localhost:5094/Home/ExportToExcel?SentData=&#39; + params + &#39;&#39;; // Passing parameter as query string

            console.log(&quot;url&quot;, url)


            var req = new XMLHttpRequest();
            req.open(&#39;POST&#39;, url, true);
            req.setRequestHeader(&#39;Content-type&#39;, &#39;application/json; charset=utf-8&#39;);
            req.send();
        }


        var response = httpPost();

Output:

XMLHttpRequest在升级到core 7后未发送

XMLHttpRequest在升级到core 7后未发送

XMLHttpRequest在升级到core 7后未发送

Note: Please refer to this official documnet for more detials. And for asp.net core FromBody

huangapple
  • 本文由 发表于 2023年7月18日 06:05:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708367.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定