[英]AJAX success callback alert not working?
我已经查看了关于 SO 的各种其他帖子,但我似乎没有看到这个问题,希望你能帮助我了解这个问题。 基本上,我正在做一个微博应用程序并在单击按钮时插入一条推文,这会调用 jQuery ajax 函数。 这是相应的代码:
home.js
这是 ajax jquery 调用
function sendTweet(single_tweet) {
var tweet_text = $("#compose").val();
tweet_text = tweet_text.replace(/'/g, "'");
tweet_text = tweet_text.replace(/"/g, """);
var postData = {
author : $("#username").text().split("@")[1], // be careful of the @! - @username
tweet : tweet_text,
date : getTimeNow()
};
$.ajax({
type : 'POST',
url : '../php/tweet.php',
data : postData,
dataType : 'JSON',
success : function(data) {
alert(data.status);
}
})
}
ajax调用成功了,推文也插入了,但是在success参数下无法获取到fireback的alert调用。 我尝试了一些基本的东西,比如alert('abc');
但它也不起作用。
tweet.php
这只是一个包装器,看起来像这样:
<?php
include 'db_functions.php';
$author = $_POST['author'];
$tweet = $_POST['tweet'];
$date = $_POST['date'];
insert_tweet($author, $tweet, $date);
$data = array();
$data['status'] = 'success';
echo json_encode($data);
?>
这只是data.status
文插入到数据库中,我想尝试将简单的 JSON 格式的数据发回,但data.status
对成功回调不起作用。
db_functions.php
这是 insert_tweet 函数所在的位置,它看起来像这样:
function insert_tweet($author, $tweet, $date) {
global $link;
$author_ID = get_user_ID($author);
$query = "INSERT INTO tweets (`Author ID`, `Tweet`, `Date`)
VALUES ('{$author_ID}', '{$tweet}', '{$date}')";
$result = mysqli_query($link, $query);
}
我已经测试过了,我很确定它运行良好。 我怀疑这是问题的原因,但如果是,我全神贯注。 我已经测试了$link
,它是在db_functions.php
文件顶部包含的另一个文件中定义的,这是有效的。
希望得到一些关于这方面的建议,谢谢!
更新
将success
更改为complete
,并且它有效。 然而, data
对象似乎有点奇怪:
data.status
在警报中弹出 200
我尝试在 PHP 中将 JSON 数组元素名称更改为data['success']
,并在前端使用data.success
访问它,并在警报框中输出:
function () {
if ( list ) {
// First, we save the current length
var start = list.length;
(function add( args ) {
jQuery.each( args, function( _, arg ) {
var type = jQuery.type( arg );
if ( type === "function" ) {
if ( !options.unique || !self.has( arg ) ) {
list.push( arg );
}
} else if ( arg && arg.length && type !== "string" ) {
// Inspect recursively
add( arg );
}
});
})( arguments );
// Do we need to add the callbacks to the
// current firing batch?
if ( firing ) {
firingLength = list.length;
// With memory, if we're not firing then
// we should call right away
} else if ( memory ) {
firingStart = start;…
这是什么意思??
更新 2
好的,我不知道这是否有帮助,但我已经从 Chrome 的检查器中打印了控制台日志,如果我没有记错的话,JSON 数据被发送回就好了。 这是整个日志:
Object {readyState: 4, getResponseHeader: function, getAllResponseHeaders: function, setRequestHeader: function, overrideMimeType: function…}
abort: function ( statusText ) {
always: function () {
complete: function () {
arguments: null
caller: null
length: 0
name: ""
prototype: Object
__proto__: function Empty() {}
<function scope>
done: function () {
error: function () {
fail: function () {
getAllResponseHeaders: function () {
getResponseHeader: function ( key ) {
overrideMimeType: function ( type ) {
pipe: function ( /* fnDone, fnFail, fnProgress */ ) {
progress: function () {
promise: function ( obj ) {
readyState: 4
responseJSON: Object
status_success: "success"
__proto__: Object
responseText: "{"status_success":"success"}"
status_success: "success"
__proto__: Object
responseText: "{"status_success":"success"}"
setRequestHeader: function ( name, value ) {
state: function () {
status: 200
statusCode: function ( map ) {
statusText: "OK"
success: function () {
then: function ( /* fnDone, fnFail, fnProgress */ ) {
__proto__: Object
__defineGetter__: function __defineGetter__() { [native code] }
__defineSetter__: function __defineSetter__() { [native code] }
__lookupGetter__: function __lookupGetter__() { [native code] }
__lookupSetter__: function __lookupSetter__() { [native code] }
constructor: function Object() { [native code] }
hasOwnProperty: function hasOwnProperty() { [native code] }
isPrototypeOf: function isPrototypeOf() { [native code] }
propertyIsEnumerable: function propertyIsEnumerable() { [native code] }
toLocaleString: function toLocaleString() { [native code] }
toString: function toString() { [native code] }
valueOf: function valueOf() { [native code] }
get __proto__: function __proto__() { [native code] }
set __proto__: function __proto__() { [native code] }
更新 3
控制台错误屏幕截图
尝试这个:
$.ajax({
type : 'POST',
url : '../php/tweet.php',
data : postData,
dataType : 'json',
complete : function(data) {
alert(data.status);
}
})
请尝试以下代码。 我在向服务器发送数据时添加了内容类型,在成功方法中使用了parseJson方法,
$.ajax({
url: "../php/tweet.php",
data: '{"author": "' + $("#username").text().split("@")[1] + '","tweet": "' + tweet_text + '","date": "' + getTimeNow() + '"}',
dataType: "json",
contentType: "application/json; charset=utf-8",
type: "POST",
success: function (data) {
var myResult = $.parseJSON(data);
},
error: function (err) {
alert(err)
}
});
如果您有任何说明,可以查看jquery ajax文档
注意:如果为error方法添加回调,它会对您有所帮助
尝试在PHP文件中设置内容类型:
header( 'Content-Type: application/json' );
然后回显你的数据:
echo json_encode( $data );
并停止脚本:
exit;
希望能帮助到你!
尝试将输入类型从“提交”更改为“按钮”。 我希望它有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.