[英]jQuery AJAX: Returns request.fail upon success
Have the following jQuery AJAX call. 具有以下jQuery AJAX调用。 It functions correctly and pulls off the PHP/MySQL operations, but returns the
request.fail
function instead of the normal done/success. 它可以正常运行并启动PHP / MySQL操作,但是返回
request.fail
函数,而不是正常的完成/成功。 Works fine if you comment/remove the MySQL query. 如果您注释/删除了MySQL查询,则工作正常。
Did a lot of searching and believe it is something to do with the dataType
, which I've tried as text
, html
, and ""
according to other posts. 进行了大量搜索,并认为这与
dataType
,根据其他文章,我尝试将其作为text
, html
和""
进行尝试。 Tried using html
as the dataType
and just changing the echo "Success.";
尝试使用
html
作为dataType
并仅更改echo "Success.";
for standard HTML/text output after the PHP code, but no luck. 用于PHP代码之后的标准HTML /文本输出,但是没有运气。 Any help would be appreciated.
任何帮助,将不胜感激。
This code is a small portion of a much larger project that has been isolated for the sake of the question. 这段代码只是为解决问题而被隔离的大型项目的一小部分。 The $variable is just an example and is within scope (and not causing an issue).
$ variable只是一个示例,在范围内(不会引起问题)。
Calling File: 调用文件:
<script type="text/javascript">
function ajaxCall() {
var request = $.ajax({
url: '<?php echo "ajax.php?variable=$value"; ?>',
type: "GET",
dataType: "html"
});
request.done(function(msg) {
alert(msg);
});
request.fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
}
</script>
AJAX File: AJAX文件:
<?php
$variable = $_GET['variable'];
$sign = mysqli_query(mysqli_connect('localhost', 'user', 'password', 'database'), "INSERT INTO `Table` (`Column`) VALUES ('$variable')");
echo "Success.";
?>
Like I said above, you need to work on error reporting for this all to work as you want it. 就像我在上面说的那样,您需要进行错误报告,以使所有这些工作都能按需进行。 I would start by building errors coming back from your
PHP
, then building the result back into JSON
like this: 我将首先构建来自
PHP
错误,然后将结果构建回JSON
如下所示:
<?php
ini_set('display_errors', false);
$variable = $_GET['variable'];
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
$json_arr['response'] = FALSE;
$json_arr['message'] = "Connection failed: " . $conn->connect_error;
} else {
$sql = "INSERT INTO `tbl1` (`col1`) VALUES ('$variable')";
$result = $conn->query($sql);
if ($result) {
$json_arr['response'] = TRUE;
$json_arr['message'] = "success";
}else{
$json_arr['response'] = FALSE;
$json_arr['message'] = $conn->error;
}
}
$conn->close();
header('Content-Type: application/json');
echo json_encode($json_arr);
Once that is set, you can then use AJAX to obtain your results in the following manner: 设置好之后,您就可以使用AJAX通过以下方式获取结果:
function ajaxCall() {
var request = $.ajax({
url: '<?php echo "success.php?variable=$value"; ?>',
type: "GET",
dataType: "json"
});
request.done(function (result) {
if (result.response) {
alert(result.message);
// your success code
} else {
alert(result.message);
// your fail code
}
});
request.fail(function (jqXHR, textStatus, error) {
alert("FAIL");
console.log(textStatus);
console.log(error);
});
}
ajaxCall();
/* js */ / * js * /
function callFunction() {
$.ajax({
url: <url>
type: 'GET',
data: {},
dataType: 'json',
success: function(response) {
alert(response.msg)
},
error: function() {
alert('Error!');
}
});
return false;
}
/* php */ / * PHP * /
echo json_encode([
'msg' => 'Message'
]);
exit
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.