Search code examples
phpjavascriptajax

ajax call to php script not working


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

Solution

  • 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.