Загрузка...

What is different in my Ajax vs XMLHttpRequest Call that lets my Server understand Ajax but not XML

Hello everyone! I hope this video has helped solve your questions and issues. This video is shared because a solution has been found for the question/problem. I create videos for questions that have solutions. If you have any other issues, feel free to reach out to me on Instagram: https://www.instagram.com/ky.emrah

Below, you can find the text related to the question/problem. In the video, the question will be presented first, followed by the answers. If the video moves too fast, feel free to pause and review the answers. If you need more detailed information, you can find the necessary sources and links at the bottom of this description. I hope this video has been helpful, and even if it doesn't directly solve your problem, it will guide you to the source of the solution. I'd appreciate it if you like the video and subscribe to my channel!What is different in my Ajax vs XMLHttpRequest Call that lets my Server understand Ajax but not XMLHttpRequest?

I have a very simple server call like this:
[HttpPost]
[AllowAnonymous]
public JsonResult Test(TestRequestModel requestModel)
{
//do stuff
return Json(new { result.Success });
}

[HttpPost]
[AllowAnonymous]
public JsonResult Test(TestRequestModel requestModel)
{
//do stuff
return Json(new { result.Success });
}

My TestRequestModel looks like this:
public class TestRequestModel
{
public string Email { get; set; } = string.Empty;
}

public class TestRequestModel
{
public string Email { get; set; } = string.Empty;
}

I am trying to do a POST request to the server. But for a complex list of reasons I need to be using XMLHttpRequest instead of $.ajax. To do this I am going to show you how I did it in ajax and then how I did it with XMLHttpRequest.
First here is how I call my server:
function MyTestFunction() {
let parameters = {
Email: "[email protected]"
}

CallServer(function () {
//Do stuff
}, function () {
//Do other stuff
}, "/Home/Test", parameters)
}

function MyTestFunction() {
let parameters = {
Email: "[email protected]"
}

CallServer(function () {
//Do stuff
}, function () {
//Do other stuff
}, "/Home/Test", parameters)
}

[email protected]
Ajax:
function CallServer(resolve, reject, path, parameters) {
$.ajax({
type: "POST",
url: path,
data: AddAntiForgeryToken(parameters),
success: function (response) {
//do stuff
},
error: function (xhr) {
//do stuff
},
complete: function () {
//do more stuff
}
});
}

function CallServer(resolve, reject, path, parameters) {
$.ajax({
type: "POST",
url: path,
data: AddAntiForgeryToken(parameters),
success: function (response) {
//do stuff
},
error: function (xhr) {
//do stuff
},
complete: function () {
//do more stuff
}
});
}

XMLHttpRequest Way:
function CallServer(resolve, reject, path, parameters) {
let xhr = new XMLHttpRequest();

xhr.open("POST", path, true);
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.setRequestHeader('RequestVerificationToken', GetMyToken());
xhr.onreadystatechange = function (e) {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200 || xhr.status === 201) {
//do stuff
}
else {
//do other stuff
}
}
};

xhr.send(JSON.stringify(parameters));
}

function CallServer(resolve, reject, path, parameters) {
let xhr = new XMLHttpRequest();

xhr.open("POST", path, true);
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.setRequestHeader('RequestVerificationToken', GetMyToken());
xhr.onreadystatechange = function (e) {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200 || xhr.status === 201) {
//do stuff
}
else {
//do other stuff
}
}
};

xhr.send(JSON.stringify(parameters));
}

If I run the above code the ajax way, then it works without issues. If I try to do it the XMLHttpRequest way then my request Source of the question:
https://stackoverflow.com/questions/79004414

Question and source license information:
https://meta.stackexchange.com/help/licensing
https://stackoverflow.com/

Видео What is different in my Ajax vs XMLHttpRequest Call that lets my Server understand Ajax but not XML канала Emrah KAYA
Яндекс.Метрика

На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.

Об использовании CookiesПринять