1

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
        }
});
4

9 回答 9

2

How would you know if it's calling it? Add a javascript alert statement to your callback. E.g

alert(data);

It will show what you echoed from the PHP script, if you got everything right.

于 2012-10-28T10:42:35.247 回答
1

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.

于 2012-10-28T10:42:52.517 回答
1

Try something like .. passing data like below

 $.ajax({
                    url: "deleteitem.php",
                    type: "POST",
                     data: { 
                          'task': 'deleteArtwork', 
                          'id': artworkObjectID
                     },              
                    success: function (msg) {

                    }
                });
于 2012-10-28T10:44:41.650 回答
1

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.

于 2012-10-28T10:49:21.830 回答
1

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.

于 2012-10-28T10:50:44.783 回答
1

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.

于 2012-10-28T10:53:11.063 回答
0

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);
})
于 2012-10-28T10:49:29.377 回答
0

Make sure the file you has created is having valid file Type like PHP file or just copy paste the existing valid working php file and write you ajax code in it. It will work now. myAjax file shown in below image is invalid file and material is valid PHP fileenter image description here

于 2020-12-29T13:57:13.240 回答
-1
url: 'deleteitem.php?task=deleteArtwork&id='+artworkObjectID 

should be

url: 'deleteitem.php?task=deleteArtwork&id='.artworkObjectID,
于 2013-08-25T17:30:46.717 回答