Search code examples
asp.net-corexmlhttprequestasp.net-core-7.0

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?


Solution

  • 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:

    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); // Creating Demo Data 
    
                }
    
                var params = JSON.stringify(dataPacket);
                console.log("param",params)
                var url = 'http://localhost:5094/Home/ExportToExcel?SentData=' + params + ''; // Passing parameter as query string
    
                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();
    

    Output:

    enter image description here

    enter image description here

    enter image description here

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