简体   繁体   English

ajax调用php脚本不起作用

[英]ajax call to php script not working

I am trying to call a PHP script using AJAX.我正在尝试使用 AJAX 调用 PHP 脚本。 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.我在我的 deleteitem.php 脚本中放置了一个简单的 echo 警报来检查它是否被调用,但无论我做什么,它都不会被调用。 The PHP script is in the same folder as the js script that is calling it PHP 脚本与调用它的 js 脚本位于同一文件夹中

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 .javascript警报语句添加到您的callback Eg例如

alert(data);

It will show what you echoed from the PHP script , if you got everything right.如果一切正常,它将显示您从PHP script回显的内容。

The output of the PHP script will be placed in data (assuming that the request is successful). PHP 脚本的输出会放在data (假设请求成功)。

It won't do anything until you replace the comment //do something here with some code that does something.它不会做任何事情,直到你用一些可以做某事的代码替换注释//do something here

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.大多数时候(无论如何我自己)当我注意到 ajax 没有更新内容时,通常是我的脚本调用的 PHP 错误。 This method will alert any errors that PHP throws on that page :)此方法将提醒 PHP 在该页面上引发的任何错误:)

Also, try and check your browser console and see if there are any errors from your javascript.另外,请尝试检查您的浏览器控制台,看看您的 javascript 是否有任何错误。

As Amitd said, you shouldn't combine GET and POST requests.正如 Amitd 所说,你不应该结合GETPOST请求。 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:如果您仍然没有收到任何响应,则可能是服务器错误,因此您应该在 .php 脚本中,在脚本开始处放置这样的行:

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.如果有任何消息,您可以检查您的 .log 文件(如果它在 apache 服务器上运行,则可以在 apache 日志文件/文件夹中找到它)。

To check whether the server code is called or not, you can check passed parameters and the AJAX response in Firebug.要检查服务器代码是否被调用,您可以在 Firebug 中检查传递的参数和 AJAX 响应。 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.首先,您没有输出存储在(您的案例) data的返回数据此外,您正在解析一些 GET 变量,尽管您的 AJAX 请求是 POST。 If you're working with variables over AJAX, i recomend using GET:如果您通过 AJAX 使用变量,我建议使用 GET:

$.get('deleteitem.php?task=deleteArtwork&id='+artworkObjectID, function (data) {
    //On success, perhaps print the returned data:
    alert(data);
})

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.确保您创建的文件具有有效的文件类型,例如PHP 文件,或者只是复制粘贴现有的有效工作 php 文件并在其中写入 ajax 代码。 It will work now.它现在会起作用。 myAjax file shown in below image is invalid file and material is valid PHP file图中显示的myAjax 文件是无效文件,材料是有效的 PHP 文件在此处输入图片说明

url: 'deleteitem.php?task=deleteArtwork&id='+artworkObjectID 

应该

url: 'deleteitem.php?task=deleteArtwork&id='.artworkObjectID,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM