0

I am using this code to obtain some data from a php file. The php file has very simple coding and I have checked that it is working efficiently. It looks like that the error is in my javascript code which is not sending the request at all. My ajax code is posted below:

var jax=new XMLHttpRequest();

jax.onreadystatechange = function() {
    if (jax.readyState == 4 && jax.status == 200)
    alert(jax.responseText);
  }

jax.open("GET","http://marked.byethost12.com/response.php?req=1&rnd="+Math.random(),true);
jax.send();

And the code in php file is this:

<?php
$request=$_GET["req"];
if($request=="1")   //requesting the initiation of protocol
    echo 'alert("hello. the protocol has been initiated!")';
else
    echo "alert! error in req variable. variable not present or value is not 1";
?>
Youstay Igo
  • 321
  • 2
  • 11
  • 1
    What's the address (including sub-domain, directory, etc) of the script? Are there any errors in your browser's developer console? – Anthony Grist Nov 05 '15 at 10:59
  • I am using the script (ajax file) from my local hard drive, not uploaded to any web server. The error string in Firefox's dev console reads thus: ***Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://marked.byethost12.com/response.php?req=1&rnd=0.1276921559338826. (Reason: CORS header 'Access-Control-Allow-Origin' missing).*** – Youstay Igo Nov 05 '15 at 11:02
  • you may encountering an cross-domain problem. look at this: http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain – Marks Nov 05 '15 at 11:03

1 Answers1

0

I think the problem is that you're sending it as an async-call. If you do like this:

jax.open("GET","http://marked.byethost12.com/response.php?req=1&rnd="+Math.random(),false);

You will see a response if you try it out in the browser-console.

Edit: As mentioned in other comments, CORS is a different beast you'll have to handle as well.

Coss
  • 453
  • 1
  • 4
  • 12
  • If I send it as a synchronous call, I will be hanging the browser functionality for a long **long** time as the page needs to communicate to the php script for several times and it might take nearly 40 seconds for the whole communication to finish. From a consumer point of view, you will get extremely fed up with the page being unresponsive for so long. That is why I am using the (very much advised by experts) true and making an async call. – Youstay Igo Nov 05 '15 at 11:09
  • I realise I phrased myself badly. What I meant was that you didn't handle the async function, where setting it to false would be a simpler demonstration to see if it works in the browser-console. Making it a synchronous call in the browser would be a bad idea indeed. For the record though, I had no problem running the code you provided. I'm just running into a CORS-issue. – Coss Nov 05 '15 at 12:23
  • Haha. Yes. Its' the CORS thats messing things up. I didn't know it was an issue till today when this ugly awakening hit me. I have modified my php file to include the line **header('Access-Control-Allow-Origin: *');** right after – Youstay Igo Nov 05 '15 at 14:01
  • You have to enable it on the server. http://enable-cors.org/server.html might be a place to start. Also, looking into JSONP could be a good idea. – Coss Nov 06 '15 at 09:58
  • I read about JSONP and it says that you can't get text data through that. I will read about the CORS thing in detail. I added the access-control-allow-origin header and set it to *. Things are still not working for me though :( I will read the website you have linked and report again after I try their approach. Both the server and client scripts are my own and I can modify them to my liking, so it is not like I am trying to illegally access any third party site through ajax :( – Youstay Igo Nov 06 '15 at 10:27
  • Failure. That site instructs nothing more than adding the header line at the start of the php script, which I already have done to no avail :( – Youstay Igo Nov 06 '15 at 10:48