javascript – AJAX成功回调警报不起作用?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – AJAX成功回调警报不起作用?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4599字,纯文字阅读大概需要7分钟。
内容图文
![javascript – AJAX成功回调警报不起作用?](/upload/InfoBanner/zyjiaocheng/771/d9cc7bbdd2ea4fdab9bd4a5ae26a5770.jpg)
我已经查看了其他各种帖子,但我似乎没有看到问题,并希望你能帮助我解决这个问题.基本上,我正在进行微博应用并在单击按钮时插入推文,该按钮调用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(‘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);
?>
这只是将推文插入数据库,我想尝试发回简单的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文件顶部包含的另一个文件中定义,这是有效的.
非常感谢有关此的一些建议,谢谢!
UPDATE
改变成功完成,它的工作原理.但是,数据对象似乎有点奇怪:
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
控制台错误scrn拍摄
解决方法:
试试这个:
$.ajax({
type : 'POST',
url : '../php/tweet.php',
data : postData,
dataType : 'json',
complete : function(data) {
alert(data.status);
}
})
内容总结
以上是互联网集市为您收集整理的javascript – AJAX成功回调警报不起作用?全部内容,希望文章能够帮你解决javascript – AJAX成功回调警报不起作用?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。