javascript ajax调用php脚本不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13108093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ajax call to php script not working
提问by vedran
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
我正在尝试使用 AJAX 调用 PHP 脚本。我在我的 deleteitem.php 脚本中放置了一个简单的 echo 警报来检查它是否被调用,但无论我做什么,它都不会被调用。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
}
});
回答by Chibuzo
How would you know if it's calling it? Add a javascript
alert statement to your callback
. E.g
你怎么知道它是否在调用它?添加javascript
警告声明您callback
。例如
alert(data);
It will show what you echoed from the PHP
script
, if you got everything right.
PHP
script
如果一切正常,它将显示您从 中回显的内容。
回答by Quentin
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
用一些可以做某事的代码替换注释。
回答by Menztrual
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 :)
大多数时候(无论如何对我自己),当我注意到 ajax 没有更新内容时,通常是我的脚本调用的 PHP 错误。此方法将提醒 PHP 在该页面上引发的任何错误:)
Also, try and check your browser console and see if there are any errors from your javascript.
另外,请尝试检查您的浏览器控制台,看看您的 javascript 是否有任何错误。
回答by Danilo Radenovic
As Amitd said, you shouldn't combine GETand POSTrequests. The code should look like:
正如 Amitd 所说,你不应该结合GET和POST请求。代码应如下所示:
$.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 日志文件/文件夹中找到它)。
回答by Nilambar Sharma
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.
要检查服务器代码是否被调用,您可以在 Firebug 中检查传递的参数和 AJAX 响应。我想这会很方便。
回答by Robin Jonsson
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:
首先,您没有输出存储在(您的案例)中的返回数据data
此外,您正在解析一些 GET 变量,尽管您的 AJAX 请求是 POST。如果您通过 AJAX 使用变量,我建议使用 GET:
$.get('deleteitem.php?task=deleteArtwork&id='+artworkObjectID, function (data) {
//On success, perhaps print the returned data:
alert(data);
})
回答by Amitd
Try something like .. passing data like below
尝试类似..传递如下数据
$.ajax({
url: "deleteitem.php",
type: "POST",
data: {
'task': 'deleteArtwork',
'id': artworkObjectID
},
success: function (msg) {
}
});
回答by rem4ik4ever
url: 'deleteitem.php?task=deleteArtwork&id='+artworkObjectID
should be
应该
url: 'deleteitem.php?task=deleteArtwork&id='.artworkObjectID,