Failing to get results using XMLHttpRequest - javascript

I'm trying to retrieve a file using XMLHttpRequest with Javascript. I'm having some problems getting the data of the sample file.
Here is the simple code:
<script type="text/javascript">
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function()
{
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
alert(xmlHttp.responseText);
}
xmlHttp.open("GET", "http://humanstxt.org/humans.txt", true);
xmlHttp.send(null);
</script>
With the above code I don't get any alerts. If I remove && xmlHttp.status == 200 then I get an empty alert, (I don't know why the server isn't giving the 200 status code). I tried different URLs but they all give out the same results.
Any thoughts on this?

Related

Ajax fails to find PHP file location

I have a simple code in a Javascript file that calls a PHP file using Ajax. At the moment it's a simple call and I don't need to pass any variables.
I'm using IIS and the folders structure is the following:
ROOT > sendMail.php
ROOT > JS > chat.js
The call starts from chat.js and it's the following:
function sendEmail(params){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("POST", "sendMail.php?q=" + params, true);
xmlhttp.send();
}
But I receive "404 Not Found". Someone can help me?
Thanks in advance!

How to access Django Service(hosted on another IP) from chrome extension

I have hosted DjangoServices on particular IP address (xx.xx.x.x:8000) and want to access them from chrome extension. I am unable to access those services while making a connection from javascript like as follows:
xmlhttp = new XMLHttpRequest();
theUrl = "http://xx.xx.x.x:8000/polls/neo4jSample/";
xmlhttp.open("POST", theUrl, true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
}
}
But, this works when I hosted on local machine as follows:
xmlhttp = new XMLHttpRequest();
theUrl = "http://127.0.0.1:8000/polls/neo4jSample/";
xmlhttp.open("POST", theUrl, true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
}
}
Please, anyone can help me out of this issue. Thanks in advance.
You need to set the hosted api base url in your manifest.json file of your extension like the following.
"permissions": [
...
"https://dummy-site-xyz.com/*",
...
],

XMLHttpRequest appending payload to the uri

I'm making a simple POST XMLHttpRequest, it is successfully posting the data but also the paylod getting appended to the URL. Is there anyway the data doesnt appended to the URL
var payLoad= JSON.stringify(ItemJSON);
var xmlhttp = new XMLHttpRequest();
xmlhttp.addEventListener("readystatechange", function () {
if (xmlhttp.readyState < 4){
}
else if (xmlhttp.readyState === 4) {
if (xmlhttp.status == 200 && xmlhttp.status < 300){
console.log("Success",xmlhttp.responseText);
}
}else{
alert("Error!! \\n There was an error while saving. Please retry again later.");
}
});
xmlhttp.open("POST", URL, false);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(payLoad);
}
Response :
https://sample.com/api/apipipelines?apiName=asdasdaaaaaa&orgUnit=ASA&apiProductName=asdasd&leadDeveloperName=Sandeep&leadDeveloperEmail=sa11as1%40sandsssy.com&baseUrl=%2Fapplication%2Foip%2Fv1%2Fcase-management%2F&projectOwnerName=alisa&projectOwnerEmail=sa%40sasssssndy.com
It is because the header that you used while posting the content:
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
Change it to
xmlhttp.setRequestHeader("Content-Type", "application/json");
Reference for you: What is the difference between form-data, x-www-form-urlencoded and raw in the Postman Chrome application?

Song didn't play when the url isn't empy

I'm really confused and got stuck building a music game using Phaser js framework.
I need the path audio which saved in localhost. To get that path I'm using AJAX. Here's my AJAX code in phaser :
function preload()
{
game.load.audio('song', pathsong);
}
function getSong()
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
pathsong = xmlhttp.responseText;
alert("succes");
alert(pathsong);
}
else
{
alert(xmlhttp.readyState);
alert(xmlhttp.status);
}
};
xmlhttp.open("GET", "getsong.php" , true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send();
}
And here's my getsong.php
<?php
$path = "asset/audio/music/1.mp3";
echo ($path);
?>
The xmlhttp.readyState and xmlhttp.status return 4 and 200 in a while and give success alert. But the song still didn't play, here's the screenshot of it.
The stop text is an alert that the song didn't play.
I can't understand why it didn't play even that var pathsong is not null.
I'm newbie at phaser and still learning about programming.
Please don't blame me, and sorry if my english is bad. I'm really trying hard to explain. ^^v
Probably you should run the preload() method right after redefining the pathsong variable when AJAX response has been successfully received.
Try to add preload(); after the line pathsong = xmlhttp.responseText;

IE throwing "Unspecified Error" with AJAX call?

Here is an example of my code:
function blah()
{
var xmlHttp = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xmlHttp.onreadystatechange = function()
{
if (xmlHttp.status == 200 && xmlHttp.readyState == 4)
alert(xmlHttp.responseText);
}
xmlHttp.open("GET", "ajax.php?_=" + new Date().getTime(), true);
xmlHttp.send();
}
It seems to work find in Chrome but IE8 throws an "Unsepcified Error" on the if status = 200 and readyState = 4 line.
Oddly, though, the alert does give the response from the php page.
Any ideas why this might be happening?
Maybe checking status if readyState != 4 causes the error in IE?
Try
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)

Categories