英文:
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();
输出:
注意: 请参考官方文档以获取更多详细信息。并且了解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 <= 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:
Note: Please refer to this official documnet for more detials. And for asp.net core FromBody
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论