jQuery Ajax Success 和 Error 函数失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8918248/
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 Success and Error function failure
提问by michael
I am having trouble getting my jQuery ajax to work properly. It directs to the PHP page to update the database, but never returns back to the script for the success or error options.
我无法让我的 jQuery ajax 正常工作。它指向 PHP 页面以更新数据库,但从不返回成功或错误选项的脚本。
My code is below:
我的代码如下:
$(document).ready(function(){
$("form#updatejob").submit(function() {
function textreplace(x) {return text.replace(/[-[\]{}()*+?.,\^$|#\s]/g, "\$&");}
// we want to store the values from the form input box, then send via ajax below
var job = $("#job").attr("value");
var description = $("#description").val();
description.replace(/[-[\]{}()*+?.,\^$|#\s]/g, "\$&");
var startDate = $("#startDate").attr("value");
var releaseDate = $("#releaseDate").attr("value");
var status = $("#status").attr("value");
$.ajax({
beforeSend:textreplace(description),
type: "POST",
url: "updatedjob.php",
data: "jobID="+ job +"& description="+ description +"& startDate="+ startDate +"& releaseDate="+ releaseDate +"& status="+ status,
success: function(){
$("form#updatejob").hide(function(){$("div.success").fadeIn();});
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Status: " + textStatus); alert("Error: " + errorThrown);
}
});
return false;
});
});
And the PHP:
和 PHP:
<?php
include("connect.php");
$job = trim($_POST['job']);
$startDate = trim($_POST['startDate']);
$releaseDate = trim($_POST['releaseDate']);
$mysqlstartdate = date('Y-m-d', strtotime($startDate));
$mysqlreleasedate = date('Y-m-d', strtotime($releaseDate));
$description = trim($_POST['description']);
$status = trim($_POST['status']);
$update = "UPDATE jobs SET startDate = '$mysqlstartdate', releaseDate = '$mysqlreleasedate', description = '$description', status = '$status' WHERE jobID = '$job' ";
$rsUpdate = mysql_query($update);
// or die(mysql_error()); mysql_close();
?>
回答by Matt Bradley
Try this:
尝试这个:
$.ajax({
beforeSend: function() { textreplace(description); },
type: "POST",
url: "updatedjob.php",
data: "jobID="+ job +"& description="+ description +"& startDate="+ startDate +"& releaseDate="+ releaseDate +"& status="+ status,
success: function(){
$("form#updatejob").hide(function(){$("div.success").fadeIn();});
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Status: " + textStatus); alert("Error: " + errorThrown);
}
});
The beforeSend
property is set to function() { textreplace(description); }
instead of textreplace(description)
. The beforeSend
property needs a function.
该beforeSend
属性设置为function() { textreplace(description); }
而不是textreplace(description)
。该beforeSend
属性需要一个函数。
回答by Max
One also may use the following to catch the errors:
也可以使用以下方法来捕获错误:
$.ajax({
url: url,
success: function (data) {
// Handle success here
$('#editor-content-container').html(data);
$('#editor-container').modal('show');
},
cache: false
}).fail(function (jqXHR, textStatus, error) {
// Handle error here
$('#editor-content-container').html(jqXHR.responseText);
$('#editor-container').modal('show');
});
回答by Stacy Drennan
I was having the same issue and fixed it by simply adding a dataType = "text" line to my ajax call. Make the dataType match the response you expect to get back from the server (your "insert successful" or "something went wrong" error message).
我遇到了同样的问题,只需在我的 ajax 调用中添加一个 dataType = "text" 行即可修复它。使 dataType 匹配您希望从服务器返回的响应(您的“插入成功”或“出现问题”错误消息)。
回答by Mian
You can implement error-specific logic as follows:
您可以按如下方式实现特定于错误的逻辑:
error: function (XMLHttpRequest, textStatus, errorThrown) {
if (textStatus == 'Unauthorized') {
alert('custom message. Error: ' + errorThrown);
} else {
alert('custom message. Error: ' + errorThrown);
}
}
回答by Robert Greene
This may be an old post but I realized there is nothing to be returned from the php and your success function does not have input like as follows, success:function(e){}
. I hope that helps you.
这可能是一篇旧帖子,但我意识到 php 没有返回任何内容,并且您的成功函数没有如下输入,success:function(e){}
. 我希望这对你有帮助。
回答by dinjas
This may not solve all of your problems, but the variable you are using inside your function (text) is not the same as the parameter you are passing in (x).
这可能无法解决您的所有问题,但是您在函数(文本)中使用的变量与您传入的参数(x)不同。
Changing:
改变:
function textreplace(x) {
return text.replace(/[-[\]{}()*+?.,\^$|#\s]/g, "\$&");
}
To:
到:
function textreplace(text) {
return text.replace(/[-[\]{}()*+?.,\^$|#\s]/g, "\$&");
}
seems like it would do some good.
似乎它会做一些好事。
回答by user3214937
You are sending a post type with data implemented for a get. your form must be the following:
您正在发送一个带有为获取实现的数据的帖子类型。您的表格必须如下:
$.ajax({
url: url,
method: "POST",
data: {data1:"data1",data2:"data2"},
...