I got this mvc core application. One of its methods looks like this(for now)
public IActionResult Call(string call)
{
Response.ContentType = "text/plain";
return Ok (call);
}
and a javascript in browser looks like that
var xhttp = new XMLHttpRequest();
xhttp.withCredentials = true;
xhttp.open("GET", request, true);
xhttp.onreadystatechange = function ()
{
if (this.readyState == 4)
{
if (this.status == 200)
{
SetAnswerText(this.responseText);
}
else
{
SetApiCallTextAreaValue("request - \n" + request + "\n failed; status = " + this.status);
}
}
else
{
SetApiCallTextAreaValue("current status is " + this.readyState + "\n" + this.responseText);
}
}
for some reason, I get only notification with readystate==3(LOADING), and never.. with readystate==4(DONE) which im expecting.
Can you help me figuring it out why thats happaning?
btw, if I open an url to this Call method in browser like https://..../Call/?call=123 it works absoulutely fine..
omg.. forgot to change function name from SetAnswerText to SetApiCallTextAreaValue..
have to sleep more..
found a error only when I looked at code in browser :P
Related
I have a var that's value is set, with an if statement to check which value, then deploy depending on the statement.
function Sort() {
var Response = document.getElementById("ResponseDiv").innerHTML;
if (Response == "Verified.") {
alert("Verified");
}
else if (document.getElementById("ResponseDiv").innerText == "Not verified.") {
alert("Not verified");
}
else {
alert("Else: " + Response);
}
}
My code isn't landing on "Verified" or "Not Verified" instead it lands on the else statement. I went through several times thinking I had a typo but isn't the same.
The ResponseDiv is filled by this (which works fine):
function Check() {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://' + Http + '/ID_' + ID + '.html', false);
xhr.setRequestHeader("Content-Type", "text/xml");
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
document.getElementById("ResponseDiv").innerHTML = xhr.responseText;
//setTimeout(Sort, 200);
Sort();
}
}
};
xhr.send(null);
}
My div gets the correct data, and the alert box in the else even gives me what I'm looking for, but lands on the else.
Debugger shows Response as coming back as "Verified.\n", but in no other way. However statement works fine when coded as if(Response == "Verified.\n").
My first post here.
I'm using droidscript and I have to include an header that contains a specific user and a password in order to retrieve a token. I'm having trouble because I don't know where to include those headers.
That's the code I'm using:
function btn_OnTouch(){
var url = "myurl";
SendRequest(url);
}
//Send an http get request.
function SendRequest(url){
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
HandleReply(httpRequest);
};
httpRequest.open("GET", url, true);
httpRequest.send(null);
app.ShowProgress("Loading...");
}
//Handle the servers reply (a json object).
function HandleReply(httpRequest){
if (httpRequest.readyState == 4){
//If we got a valid response.
if (httpRequest.status == 200){
txt.SetText("Response: " + httpRequest.status + httpRequest.responseText);
}
//An error occurred
else
txt.SetText("Error: " + httpRequest.status + httpRequest.responseText);
}
app.HideProgress();
}
The told me I should probably include the headers like this, but I don't know where to put them in my code.
httpRequest.setRequestHeader(“username”, “myuser”);
httpRequest.setRequestHeader(“password”, “mypass”);
You need to do it just after calling the open method like this:-
httpRequest.open("GET", url, true);
httpRequest.setRequestHeader("username", "myuser");
httpRequest.setRequestHeader("password", "mypass");
httpRequest.send(null);
I can't seem to receive Javascript's HTTPRequest. I get this HTTP/1.1 200 OK
but I cannot seem to get the URL that I sent over. I just want the link that I am sending over on my webpage.
Here is my javascript:
jQuery(function($) {$("button").click(function(){
console.log(this.id);
document.getElementById("direction").innerHTML = this.id + " is pressed.";
newurl = 'index.php?btn='+ this.id+"?key="+user.key;
sendHttpRequest(newurl);
});
});
function sendHttpRequest(update){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
console.log(xhttp.responseText);
}
}
xhttp.open("GET",update,true);
xhttp.send(null);
console.log(update);
}
ESP8266 in void loop:
WiFiClient client;
String url = "/index.php?";
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
Serial.println("Request Sent");
String request = client.readStringUntil('\r');
Serial.println("headers received");
Serial.println(request);
You're only reading the first line of the response from the index.php script on the ESP, which will generally be the HTTP status code you are seeing, and nothing else (Unless there's some other code you haven't posted). You need to read the entire HTTP message to get the response, specifically the body - Assuming index.php uses the body to return the data you need. You can find a description of the HTTP message structure here: https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html
I have an ajax function on my website which calls a cgi script. infrequently, this script returns a 500 error. I am trying to change the AJAX call so that when this happens, it will repeat the request. I have tried to do that as follows:
function shelverx() {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
//process data if successful...
} else if (xmlhttp.status == 500) {
limit++;
if (limit < 5) {
shelverx(usershelf);
}
}
}
xmlhttp.open("POST", filepath, true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(postdata);
}
I'm wondering if there is a better way to accomplish this? I find that what happens is that whenever there is a 500 error, the shelverx function is called four times, even if the second call is successful. Is there a way to repeat the call only once per error?
I also tried to accomplish the same thing by changing the entire ajax call to jQuery and using the following error function:
error: function(xhr, textStatus, errorThrown ) {
this.tryCount++;
if (this.tryCount<=this.retryLimit) {
$.ajax(this);
return;
}
But I got an Unexpected Token : error. If anyone could help me to get either of these methods to work I would appreciate it greatly. My goal is to have the ajax call repeat only once per 500 error.
Jonathan -
The code snippet below works as you want. The counter holds its value between calls and stops requests at 5. I also changed the IF statement logic and added a timer to wait 1 second between tries. Give it a try.
Note that the original logic doesn't work correctly. In the debugger it was incrementing the counter multiple times per each request. Much better to check status after readyState = 4.
Original Code:
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
//process data if successful...
} else if (xmlhttp.status == 500) {
limit++;
if (limit < 5) {
shelverx(usershelf);
}
}
Tested and working example:
<!doctype html>
<html>
<body>
<script type="text/javascript">
var postdata = '';
var filepath = 'NothingHere.html';
var limit = 0;
function shelverx() {
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
console.info('Success: Recieved ' + xmlhttp.responseText.length + ' bytes');
}
else {
limit++;
console.info( 'Error: ' + xmlhttp.status + ':' + xmlhttp.statusText );
if (limit < 5) setTimeout( shelverx, 1000 );
}
}
}
xmlhttp.open("POST", filepath, true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(postdata);
}
shelverx();
</script>
</body>
</html>
I use an XMLHttpRequest to post data to a server. It looks like this:
var xhr = new XMLHttpRequest();
var url = 'http://myurl/post';
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
if (xhr.status == 200) {
leaveEditMode();
} else {
initOverlay('Error: ' + xhr.statusText);
}
}
};
xhr.send(
'StringName=' + $activeElement.dataset.name + '&' +
'Text=' + encodeURIComponent($activeElement.innerHTML) + '&' +
'Language=' + userLanguage
);
This code works perfectly fine in Chrome, FireFox and Opera. But in IE11 it does not. There are no errors and I get status code 200 back from the request. But the data is not being posted. Seems like the request is ignored.
Any ideas? I tried an anti-cache-header - nothing.
Thanks for your help! :-)
The problem was, simple enough, the parameter userLanguage was not set right and the server didn't send an appropriate response.