I am trying to call a PHP script using AJAX. I have put a simple echo alert in my deleteitem.php script to check whether it is getting called, but whatever I do, it does not get called. The PHP script is in the same folder as the js script that is calling it
Can someone tell me why that would be? What are the possible causes?
$.ajax({
url: 'deleteitem.php?task=deleteArtwork&id='+artworkObjectID,
type: "POST",
dataType: "html",
success: function(data)
{
//do something here
}
});
How would you know if it's calling it? Add a javascript
alert statement to your callback
. Eg
alert(data);
It will show what you echoed from the PHP
script
, if you got everything right.
The output of the PHP script will be placed in data
(assuming that the request is successful).
It won't do anything until you replace the comment //do something here
with some code that does something.
Try something like .. passing data like below
$.ajax({
url: "deleteitem.php",
type: "POST",
data: {
'task': 'deleteArtwork',
'id': artworkObjectID
},
success: function (msg) {
}
});
The first thing I would be doing is checking the results of the success callback.
$.ajax({
url: 'deleteitem.php?task=deleteArtwork&id='+artworkObjectID,
type: "POST",
dataType: "html",
success: function(data)
{
alert(data); // Alert the results
}
});
Most times (with myself anyway) when I notice things aren't being updated by ajax is usually a PHP error with my script im calling. This method will alert any errors that PHP throws on that page :)
Also, try and check your browser console and see if there are any errors from your javascript.
As Amitd said, you shouldn't combine GET and POST requests. The code should look like:
$.ajax({
url: 'deleteitem.php?task=deleteArtwork&id='+artworkObjectID,
type: "GET",
dataType: "html",
success: function(data)
{
alert(data); // alert on success
}
});
If you still don't get any response, there might be a server error, so you should put lines like this one in the .php script, at the beggining of the script:
error_log("script was called, processing request...");
error_log("passed artworkObjectId is: " . $_GET["artworkObjectID"]);
You can then check in your .log file (it can be found in the apache log file/folder if it's running on apache server) if there are any messages.
To check whether the server code is called or not, you can check passed parameters and the AJAX response in Firebug. It would be handy I guess.
First of all, you're not outputting the returned data, stored in (your case) data
Also, you're parsing along some GET variables, although your AJAX-request is POST. If you're working with variables over AJAX, i recomend using GET:
$.get('deleteitem.php?task=deleteArtwork&id='+artworkObjectID, function (data) {
//On success, perhaps print the returned data:
alert(data);
})
url: 'deleteitem.php?task=deleteArtwork&id='+artworkObjectID
应该
url: 'deleteitem.php?task=deleteArtwork&id='.artworkObjectID,
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.