XMLHttpRequest status 0 but responseText correct
Learn the basics
Before you START programming for Web you should understand what is HTTP, WebServer, Client, Protocol, Request, Response and Status Code.
Protocol file vs http
Protocol file://
don't work with XMLHttpRequest
by browser security, prefer http://
.
For use http://
in your PC/Machine install Apache or Nginx
You use PHP, .NET, Java (jsp or jsf) or something?
To start working with "WEB" is advisable to know a programming language and perhaps a framework:
- Django (Python FrameWork) install Django 1.6.2
- Django+Windows
- PHP+Windows install Wamp
- PHP+Linux (or Windows) install Xampp
- ASP.NET (asp.net is a framework, you can programing in C# or VB) install Microsoft ASP.NET
- JSF2 (Java is language and Facelets is "template"): http://balusc.blogspot.com.br/2011/01/jsf-20-tutorial-with-eclipse-and.html
- Rails (Ruby is language and Rails is "Framework"): http://rubyonrails.org/
Ajax vs Sjax
Ajax in Async-mode requires onreadystatechange
Ajax ("A"synchronous Javascript and XML):
var request = new XMLHttpRequest();
request.open("GET", "test.html", true);//true is "async"
request.onreadystatechange = function (event) {
if (request.readyState==4) {
console.log("status: "+request.status);
console.log("response: "+request.responseText);
}
};
request.send(null);
"SJAX" ("S"ynchronous Javascript and XML):
var request = new XMLHttpRequest();
request.open("GET", "test.html", false);//false is "sync"
request.send(null);
if (request.readyState==4) {
console.log("status: "+request.status);
console.log("response: "+request.responseText);
}
Prefer asynchronous mode, so that you can work multiple events without a need to wait for the other and also avoid freezing the "javascript".
XMLHttpRequest has a 200 status, but responseText is empty
You are using XMLHttpRequest in asynchronous mode:
xhr.open('POST', cloudinaryURL, true);
But your code is designed for synchronous mode:
xhr.open('POST', cloudinaryURL, false);
In asynchronous mode, xhr.send()
will return immediately without actually processing the request. So, xhr.responseText
hasn't been populated yet when you try to access it.
XMLHttpRequest responseText is empty
its not possible with ajax because of same origin policy , but to solve your problem you can use server side programming like PHP or Nodejs. basically PHP will be available in web hostings.
create getGoogle.php
<?php
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "www.google.de");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
echo $output;
?>
use your javascript as below
<script type="text/javascript">
var http = new XMLHttpRequest();
http.open("GET", 'getGoogle.php', true);
http.send(null);
http.onreadystatechange = function() {
alert(http.responseText);
}
alert("The end is reached");
</script>
Hope this solves your problem. Cheers
xmlhttprequest status is always 0 and no response in responseText
This is a cross domain issue (CORS) which means you are trying to make a request (for a resource) from outside of your current domain. You need to allow set the Access-Control-Allow-Origin
to accept all your requests.
You can do this by adding the below lines to your .js file (where you have your express.js code)
response.writeHead(200, {
'Content-Type': 'text/plain',
'Access-Control-Allow-Origin' : '*'
});
XMLHttpRequest status 4 but responseText is empty
alert(req_url) should show wrong url "http://www.domainname.comads/verifypublisher/"
Related Topics
Is There a Version of JavaScript's String.Indexof() That Allows for Regular Expressions
Anything Similar in JavaScript to Ruby's #{Value} (String Interpolation)
How to Run JavaScript Inside Swift Code
JavaScript Keycode VS Charcode
How to Implement Inheritance in Js Revealing Prototype Pattern
Jquery Clone() Not Cloning Event Bindings, Even with On()
How to Create Streams from String in Node.Js
Access Parent's Parent from JavaScript Object
How to Force Reloading a Page When Using Browser Back Button
Non-Blocking Settimeout in JavaScript VS Sleep in Ruby
Inline Ruby in :JavaScript Haml Tag
Get Column from a Two Dimensional Array