4
I’m on a project. NET, in which I create a login page and check the DLL, and in this check, is passed a string of a message that I need to capture with a Typescript to give the alert. This message is already being sent, and it appears there in the page header, as soon as a post is made.
What I need now is to take this message to make a simple alert()
and it can appear to the user.
Here is a print that shows how the message is in the header:
I just need to pick her up and do the Alert.
Help me please Thank you.
I’ve tried this code:
var req = new XMLHttpRequest();
req.open('GET', document.location.href, false);
req.send(null);
var headers = req.getAllResponseHeaders().toLowerCase();
alert(headers);
of this question https://stackoverflow.com/questions/220231/accessing-the-web-pages-http-headers-in-javascript
but I do not see the message, only this data:
I NOTICED SOMETHING ON THE NETWORK
when I post and it returns Alert, the network looks like this:
Note that there is only 1 localhost fileand when I click on the Ubmit button, another localhost file appears that does not contain the header "message":
is created by the code req.send(null);
Observe:
The first localhost contains the message, the last one is generated by req.send(null);
In my TS code la no sources, the following error appears on the line:
req.open("HEAD", window.location.href, false);
Synchronous Xmlhttprequest on the main thread is deprecated because of its detrimental effects to the end user’s Experience. For more help, check in https://xhr.spec.whatwg.org/.
EDIT:
In my file . Cs, where I add the header, has the following code:
public async Task OnPostAsync(PARAMETROS){
CancellationTokenSource backGroundTask = new CancellationTokenSource();
MobAtivacaoAcesso MobAtivacaoAcesso = new MobAtivacaoAcesso("X", "Z");
await MobAtivacaoAcesso.AtivacaoVerificarAsync(PARAMETROS);
if (!String.IsNullOrEmpty(MobAtivacaoAcesso.MensagemTemporaria)){
Response.Headers.Add("mensagem", MobAtivacaoAcesso.MensagemTemporaria);
}
return;
}
The request is asynchronous and you are not waiting for an answer to execute the
alert
. You need a function callback to be executed when the request is completed... A MDN is a good source and see also the property documentationXMLHttpRequest.onreadystatechange
– fernandosavio
Caro @fernandosavio is not asynchronous, the third parameter is
false
no . open, indicates synchronous, so it is not used neither onload nor onreadystatechange, I recommend you read: https://answall.com/a/116177/3635– Guilherme Nascimento
This looks like a problem in your back-end, probably you wrote the header only in GET, when you receive the POST does not contain the HEADER
– Guilherme Nascimento
actually, I put the Response.header.add() just me a public async Task Onpostasync
– user149429
I tried to put the same way for an Ongetasync, but it continued the same way... @Guilhermenascimento
– user149429
It seems to me that for some reason the command req.send(null); is creating a new localhost file and is picking up the headers of this new page, which does not have the message.... I’ll add a print to the question to make it easier for you to see.
– user149429
Guys, I’m sorry about the comments. it turned out to be useless because I didn’t realize the call was synchronous. Thank you @Guilhermenascimento for the clarification and I apologize again for the lack of attention.
– fernandosavio