1

I am new to javascript and AJAX, and have spent the last 8 hours on this one problem, and its beating me. I know its simple, just can't find what I am doing wrong. I have an image on my site with a with an on-click=SendCommand() . This is the js code that I have

function SendCommand(){
        alert("BingoBango!");

        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET","https://www.justanexample.com/API/MobileAPP/SendCommand.php?apikey=7785adf3a5d3a3adsf555nb5v55bsaer5v&mac=b827eb6ffa19&command=2",true);
        xmlhttp.send();
};

I get the alert message, and I get no errors using firebug or in chrome javascript console. However that page is not executed. I can however copy and paste that exact url into the browser and it executes successfully.

Any help would be GREATLY appreciated, its kickin my butt.

  • 2
    What do you expect to happen? What do you see in the network tab? – SLaks Feb 05 '14 at 20:39
  • Are you seeing a new request appear in the browser dev tools/network tab? If not, the request isn't being sent. – helion3 Feb 05 '14 at 20:40
  • Let me check that, thanks for the quick responses!! Also, just had a thought, the ssl cert on the server is self signed, could that cause a problem? – chuckycharms Feb 05 '14 at 20:43
  • It does show that it has loaded OK. Size of the content is listed "From Cache".....Cause of the problem? – chuckycharms Feb 05 '14 at 20:44
  • 1
    What makes you think it didn't execute? Something you expect to see on the server, or something you expect to see in the browser? – Jason P Feb 05 '14 at 20:45
  • The page it is calling is calling a python script as well as updating a mysql record. This works fine and I can see the results when called directly from a browser – chuckycharms Feb 05 '14 at 20:45
  • Sorry, that wasnt very clear. I am expecting to see the python script run on my device, as well as see the mysql table get updated, It is not. I dont expect to see anything in the browser. – chuckycharms Feb 05 '14 at 20:47
  • 1
    If the browser returns a cached response, it didn't actually make the request to the server. – Jason P Feb 05 '14 at 20:47
  • is there something i can do in my request to ask it to not use the browser cache and actually hit my server? – chuckycharms Feb 05 '14 at 20:48
  • What I generally do is to get the current time in milliseconds and append that as a url parameter. – Jason P Feb 05 '14 at 20:51

2 Answers2

1

The page it is calling is calling a python script as well as updating a mysql record. Is there something i can do in my request to ask it to not use the browser cache and actually hit my server?

You should not use a GET request for things that execute actions on the server. Use POST instead, which should not be cached.

If that doesn't help, adapt your HTTP cache headers or, as a last resort, append random strings to the url.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • And you should not post comments in the answers section ;) – Niet the Dark Absol Feb 05 '14 at 20:56
  • @Niet: It's short, but it's an answer, isn't it? – Bergi Feb 05 '14 at 20:57
  • Well, now it is XD The problem is caused by cache headers. – Niet the Dark Absol Feb 05 '14 at 20:58
  • Thanks to JasonP!! My issue was that the request was getting a cached response. Adding a random number to the url worked. For example the new url is xmlhttp.open("GET","https://www.justanexample.com/API/MobileAPP/SendCommand.php?apikey=7785adf3a5d3a3adsf555nb5v55bsaer5v&mac=b827eb6ffa19&command=2&r=" + Math.random(),true); – chuckycharms Feb 05 '14 at 20:59
  • I am confused right now about the concept of "response cache", first encounter, as far as I understood right now the SERVER does not react on same domain request, if...? And how does the server identify that? @Jasonp – loveNoHate Feb 05 '14 at 21:27
0

This is how I would do it

JS

function SendCommand()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
}
xmlhttp.open("GET","https://www.justanexample.com/API/MobileAPP/SendCommand.php?apikey=7785adf3a5d3a3adsf555nb5v55bsaer5v&mac=b827eb6ffa19&command=2",true);
xmlhttp.send();
}

HTML

<button type="button" onclick="SendCommand()">My button</button>
<div id="myDiv"></div>
Kevin Lynch
  • 24,427
  • 3
  • 36
  • 37