I am not able to make a post-call using xmlhttprequest in javascript. First, I am getting a 401 response code from server-side. Below is code I am using below code.
function makeRestCall(){
console.log("Rest call made")
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
alert(this.responseText);
}
};
xhttp.open("POST", "myurk", false, 'username', 'password');
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.send()
alert(xhttp.status);
alert(xhttp.responseText)
I am also getting
"Request has been blocked by CORS policy"
I am able to do a rest call from postman and using basic auth and getting response also
I have referred various links for making a rest call using javascript.
Link 1 Link 2
But still, I am not able to resolve the error. Can anyone tell what is wrong am I doing.
Related
I'm creating a controller for lights using thingsboard.
I need to change device's telemetry data (thingsboard) using rest put request
$.post("http://<ip_here>:8080/api/v1/<device_accesscode_here>/telemetry",{ selectedPreset:2 });
REST calls work using swagger.io and postman, but when calling from widget or any other web page, request returns 400.
Can't seem to find solution to this and url is correct. i have tried both $.post and $.ajax styles.
YAY! i got it working!
for some reason, only XHR approach worked..
var data = "{\"selectedPreset\":\"2\"}";
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.open("POST", "IP HERE");
xhr.setRequestHeader("cache-control", "no-cache");
xhr.setRequestHeader("postman-token", "33c35ded-140d-e016-fa35-ee8185d7bd44");
xhr.send(mydata);
i ripped this right out of postman.
I'm using HTML macros in confluence to automate some tasks in Jira. I tried the following REST call in the code below to add a comment to a ticket and I get the error: 400: Bad Request. The request sent by the client was syntactically incorrect.
I can't see any issues with my JSON or code. This pretty much fails for other operations I've tried such as creating/updating tickets.
xmlhttp = new XMLHttpRequest();
var url = "http://<CONFLUENCEURL>.com:8090/plugins/servlet/applinks/proxy?appId=<APPID>&path=http://<JIRAURL>:8080/rest/api/2/issue/TEST-3/comment";
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("Content-type", "application/json");
xmlhttp.setRequestHeader("Accept", "application/json");
xmlhttp.onreadystatechange = function () { //Call a function when the state changes.
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert(xmlhttp.responseText);
}else{
console.log(xmlhttp.responseText);
}
}
var parameters = {
"body" : "Hello world!"
};
function addComment() {
xmlhttp.send(JSON.stringify(parameters));
}
I eventually created an identically ordered JSON object, turns out that the parser the server side uses does not follow the JSON specification in that it expects the correct order.
As in, the fix would be to follow the JSON specification server side, or replicate identically the structures being sent to the server.
I am using xmlhttprequest() in javascript to find the statuscode for the link.
xhttp = new XMLHttpRequest();
xhttp.timeout = 5000;
xhttp.open("GET", url, true);
xhttp.send(null);
I am writing this code in a function and calling this function in a for loop for almost 1000 links.If i execute this once it is getting executed and then if i launch chrome and trying to run anurl it is showing error like "Too much traffic from the server" and asking for verification.
I have done same in Java but in java(Using httprequest) i am not getting any error.
One more issue i am facing is i am getting status code as '0'.What does it mean ??
The speed of execution is also very slow.Is there any way to find the status code of an url faster using javascript???
This code is working fine for xmlhttprequest :
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// Typical action to be performed when the document is ready:
document.getElementById("demo").innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", "filename", true);
xhttp.send();
How can to request url or website address and show response code with javascript or jquery?
i.e
request www.google.com
if (response_code = 200) {
print "website alive"
} else if (response_code = 204) {
print "not found";
}
I'm assuming from the jquery tag that you mean to do this in a browser, not from a server running NodeJS or similar (although there is a NodeJS module for jQuery).
Although you can request URLs and see the response code using the XMLHttpRequest object, the Same Origin Policy will prevent your accessing virtually any sites other than the one the page itself was loaded from. But if you're pinging the server your page was loaded from to make sure it's still there, you can do that:
function ping(url, callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = handleStateChange;
xhr.open("get", url);
xhr.send();
function handleStateChange() {
if (xhr.readyState === 4) { // Request is complete
callback(xhr.status); // Tell the callback what the status code is
}
}
}
I have such part of code:
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://someurl.com", true);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
doSomeTask();
}
}
}
xhr.send('login=mylogin&password=mypassword');
How can I know if my login&password are correct? In both cases xhr.status is 200.
Invalid Login/Password attempts are not HTTP failures. Only HTTP Failures return you 4xx or 5xx return codes. You might want to use the xhr.responseText or xhr.responseXML from the response to see what your backend is returning and base your decision according to that. Please refer to http://www.w3.org/TR/2006/WD-XMLHttpRequest-20060405/#dfn-responsetext for how the responses are obtained.
Also, there are tons of good Javascript framework that hide the complexity of making Ajax calls. JQuery makes the job of calling AJAX scripts extremely easy. You might want to investigate on that instead of writing raw XHR code.