[英]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.