AJAX成功回调警报不起作用? [英] AJAX success callback alert not working?

查看:81
本文介绍了AJAX成功回调警报不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经检查了其他各种帖子,但我似乎没有看到问题,并希望你能帮助我解决这个问题。基本上,我正在进行微博应用并在单击按钮时插入推文,该按钮调用jQuery ajax函数。以下是相应的代码:

I've checked out various other posts on SO, but I don't seem to see the problem, and was hoping if you could help me shed some light on this issue. Basically, I'm doing a microblogging appliation and inserting a tweet when a button is clicked, which calls the jQuery ajax function. Here's the respective code:

home.js

这是ajax jquery调用

This is the ajax jquery call

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('abc'); ,但它也没有用。

The ajax call works successfully, and the tweet is inserted, but I can't get the alert call to fireback under the success parameter. I tried something basic like alert('abc'); but it didn't work either.

tweet.php

这只是一个包装器,如下所示:

This is just a wrapper, looks like this:

<?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);
?>

这只是将推文插入数据库,我想尝试发回简单的JSON格式数据,但 data.status 对成功回调无效。

This just inserts the tweet into the database, and I wanted to try sending simple JSON formatted data back, but data.status didn't work on the success callback.

db_functions.php

这是insert_tweet函数所在的位置,它看起来像这样:

This is where the insert_tweet function is in, and it looks like this:

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 文件顶部的另一个文件中定义的,这是有效的。

I've tested it, and I'm pretty sure this runs fine. I doubt this is the cause of the problem, but if it is, I'm all ears. I've tested the $link, which is defined in another file included in the top of the db_functions.php file, and this works.

对此表示感谢,谢谢!

UPDATE

成功更改为完成,它确实有效。但是,数据对象似乎有点奇怪:

Changed success to complete, and it works. However, the data object seems a bit odd:

data.status 在警报中弹出200

data.status pops up 200 in the alert

我尝试将JSON数组元素名称更改为 data ['success'] 在PHP中,并使用 data.success 在前端访问它,并在警告框中输出:

I tried changing the JSON array element name to data['success'] in PHP, and accessed it in the front end with data.success, and it outputted this in the alert box:

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数据被发送回来就好了。这是整个日志:

Okay, I don't know if this helps, but I've printed the console log from Chrome's inspector, and if I'm not mistaken, the JSON data is sent back just fine. Here's the entire log:

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

控制台错误scrn shot

Console error scrn shot

推荐答案

尝试这个:

$.ajax({
    type        :   'POST',
    url         :   '../php/tweet.php', 
    data        :   postData,
    dataType    :   'json',
    complete     :   function(data) {
        alert(data.status);
    }
})

这篇关于AJAX成功回调警报不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆