im trying to do a ajax port to a php file, and it seems that is doesn’t want to send the data.
i have a android project using phone gap library and it looks like if i go a get it works:
$(document).ready(function () {
$.ajax({
type: 'GET',
url: 'http://example.com/functions.php',
data: { get_param: 'login' },
dataType:'json',
success: function (data) {
alert('success');
});
},
error: function () {
console.log(XMLHttpRequest.errorThrown);
}
});
});
so, this works properly. But when i try to post tome data it doesn’t:
var vals = $("#form").serialize();
$.ajax({
type: 'POST',
url: 'http://example.com/functions.php',
data: vals,
dataType:'json',
success: function (data) {
alert('success');
$.mobile.changePage( "test.html", { transition: "slideup"} );
},
error: function () {
alert('fail');
}
});
with this previous example i get the fail alert
i’ve also tried:
$.post("http://example.com/functions.php", vals ,
function(data){ //Your onsuccess call backfunction
alert(data);
}, "json" //retun data type //can be html/json/xml/text
);
e.preventDefault();
but this fails also.
Any ideas on why the get works but not the post? Right now i have nothing returning from the php function but i should at least get the success alert.
lots of thanks
edit: the php code i have checks for the vals sent by the ajax. One of them is name
if (isset($_POST['name'])) {
return "name";
}
Your client side code looks good. Both the
$.ajaxand$.postcalls should successfully submit the form values to the server side script. From there on, it’s this script that executes and it might throw errors. I would recommend you using a javascript debugging tool such as FireBug in FireFox or Chrome developer toolbar in Google Chrome which allows you to monitor an AJAX request. You can see the actual request sent and the response from the server. You can see the HTTP status code returned by the server as well as the precise response. So if this status code is different than 200 (which is your case) theerrorcallback will execute on the client. So go debug and fix yourfunctions.phpserver side script.