I have some code which I am using to call $.ajax
function doajax(url, dataType, data, method, context, callback, block) {
block = typeof block !== 'undefined' ? block : true;
if (block) {
showOverlay();
}
var settings = {
dataType: dataType,
data: data,
type: method,
url: url,
error: onerror,
success: function (data, textStatus, jqXHR) { onsuccess(data, textStatus, jqXHR, context, callback); }
};
$.ajax(url, settings);
console.log("doajax: called $.ajax");
return false;
}
function onerror(jqXHR, textStatus, errorThrown) {
console.log("doajax: error");
hideOverlay();
formSpinner.stop();
var extra = jqXHR.hasOwnProperty("responseJSON") ? jqXHR.responseJSON.error : jqXHR.responseText;
alert("Error (" + jqXHR.status + ") :: " + textStatus + " :: " + errorThrown + " :: " + extra);
}
function onsuccess(data, textStatus, jqXHR, context, callback) {
console.log("doajax: success");
hideOverlay();
formSpinner.stop();
if (data.status === 200 || data.status === 201) {
hooks[callback](data, context);
}
else {
alert("Error (" + jqXHR.status + ") :: " + textStatus + " :: " + data.error);
}
}
Everything was working ok, but now, there is no request received by the server, and the Chrome
network window is showing me a GET 200 OK
regardless of whether it was a GET
or POST
request.
In the network tab, there is NO request which goes out for the url: url
parameter from the $.ajax settings
, instead I am seeing a request for the current page, which produces the 200 OK
response.
There is no error being thrown, and the success function is also never called.
I have been looking at possible caching issues, however, POST
requests are never cached so I am unsure what is happening.
EDIT: Added console.log()
and after performing a UI function which should trigger the $.ajax()
call the log is showing:
doajax: called $.ajax
I have also turned on Log XMLHttpRequests
and I am seeing confirmation that the request does in fact generate but for the current page url
, not the parameter passed in via the settings
Answer: This is an issue with jQuery
versions. I found there is an old version of jQuery
being used where I have just integrated this code. When I drop jQuery 1.11
in on top of my code, it is all working ok.
So the actual problem is that until jQuery 1.5
there was no support for $.ajax(url, settings);
and only $.ajax(settings);
can be used.
I can confirm that both using jQuery 1.5+
or changing my code to be $.ajax(settings);
will work and fix the problem.