This is my function to call ajax:
var sendAjax = function(type, data, callback) {
$.ajax({
url: homepage + 'server_ajax.php',
type: 'POST',
data: {
type: type,
data: JSON.stringify(data)
}
}).done(function(data) {
console.log(data);
callback(true, JSON.parse(data));
}).fail(function() {
console.log('NO');
callback(false, {});
});
};
Variable homepage
contains valid URL. It is same domain, so this is not the problem.
My server_ajax.php
has only this: echo "awdawda wawddawawd";
- nothing more (testing purposes).
I tried to put breakpoints to $.ajax...
and to the console logs. First breakpoint works, but functions done
and fail
never run.
No error show up in the browser console
My jQuery is include (I'm sure), I use require.js
and everything else in this JS file using jQuery works well.
I also tried change URL to something invalid (like server_ajax2.php
) and console prints error 404 - like it would.
Also tried with dataType: 'json'
or removing data
object...
Any ideas, what should I try? I also tried some answers on this site (don'r flag this as duplicate, I tried like everything), but nothing helped me.
Have you tried doing the following on the server_ajax.php
file:
echo json_encode('awdawda wawddawawd');
header('X-PHP-Response-Code: 200', true, 200);
This encodes the response as JSON so that your JavaScript code can read the content, and gives the standard success header (200 OK
) to the page so that the $.ajax
code knows the query was successful.
Here try this, since your getting data back perhaps its your success failure functions.
$.ajax({
url: homepage + 'server_ajax.php',
type: 'POST',
dataType : 'json',
contentType: 'text/plain',
data: {
type: type,
data: JSON.stringify(data)
},
success: function(data) {
console.log(data);
callback(true, JSON.parse(data));
},
error: function() {
console.log('NO');
callback(false, {});
}
});