Jquery $.ajax Post To Php File Not Working
Solution 1:
I think this may be the error:
data: aid,
it should be
data: {'aid':aid},
That will provide the $_POST['aid'] label and value you're looking for in your php page.
EDIT:
If you're having trouble with this, I'd simplify it for testing, the main reasons for things like this not working in my experience are:
it's not reaching the file
it's reaching the file but the file's expecting something different than it's receiving and due to control structures it's not returning anything
it's reaching the file with the correct data, but there are other errors in the php file that are preventing it from ever returning it.
You can easily rule out each of these in your case.
Solution 2:
The jQuery data parameter to the ajax method should be an object, such as {'aid': aid}
. I think that's your problem. I also noticed your always method is missing a parameter for data.
Solution 3:
If you're now posting the data correctly could the problem be in your PHP page? The init.php include couldn't be changing the value of $_POST['aid'] could it?
Solution 4:
I'm not sure done is suppose to behave like that, normally you use done like this:
$.ajax({
//...
})
.done(function(data){
//...
});
Just do it like this:
var fetch = true;
var url = 'someurl.php';
$.ajax(
{
// Post the variable fetch to url.
type : 'post',
url : url,
dataType : 'json', // expected returned data format.data :
{
'aid' : aid
},
success : function(data)
{
// This happens AFTER the backend has returned an JSON array (or other object type)var res1, res2;
for(var i = 0; i < data.length; i++)
{
// Parse through the JSON array which was returned.// A proper error handling should be added here (check if// everything went successful or not)
res1 = data[i].res1;
res2 = data[i].res2;
// Do something with the returned data
}
},
complete : function(data)
{
// do something, not critical.
}
});
You can add the failure part in if you want. This is, anyway, guaranteed to work. I'm just not familiar enough with done to make more comments about it.
Solution 5:
Try
include"{$_SERVER['DOCUMENT_ROOT']}/TrakFlex/core/init.php";
instead cause $_SERVER[DOCUMENT_ROOT] witout bracket won't work. When you are using an array in a string, you need those brackets.
Post a Comment for "Jquery $.ajax Post To Php File Not Working"