Cod*_*ice 5 javascript php twitter ajax jquery
我已经查看了有关SO的其他各种帖子,但是我似乎看不到问题,希望您能帮助我阐明这个问题。基本上,我正在执行微博应用程序,并在单击按钮时插入一条推文,这将调用jQuery ajax函数。以下是相应的代码:
home.js
Run Code Online (Sandbox Code Playgroud)
这是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);
}
})
}
Run Code Online (Sandbox Code Playgroud)
ajax调用成功完成,并且插入了tweet,但是我无法在成功参数下获得对回火的警报调用。我尝试了一些基本的操作,alert('abc');但还是没有用。
tweet.php
Run Code Online (Sandbox Code Playgroud)
这只是一个包装,看起来像这样:
<?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);
?>
Run Code Online (Sandbox Code Playgroud)
这只是将推文插入数据库中,我想尝试发送回简单的JSON格式的数据,但是data.status在成功回调中不起作用。
db_functions.php
Run Code Online (Sandbox Code Playgroud)
这是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);
}
Run Code Online (Sandbox Code Playgroud)
我已经对其进行了测试,并且我可以肯定它运行良好。我怀疑这是问题的原因,但如果是的话,我全神贯注。我已经测试了$link,该文件在文件顶部的另一个文件中定义db_functions.php,并且可以正常工作。
希望对此有一些建议,谢谢!
更新
更改success为complete,并且可以使用。但是,该data对象似乎有些奇怪:
data.status 在警报中弹出200
我尝试data['success']在PHP 中将JSON数组元素名称更改为,并使用对其进行了前端访问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;…
Run Code Online (Sandbox Code Playgroud)
这是什么意思??
更新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] }
Run Code Online (Sandbox Code Playgroud)
更新3
控制台错误屏幕快照

小智 2
尝试这个:
$.ajax({
type : 'POST',
url : '../php/tweet.php',
data : postData,
dataType : 'json',
complete : function(data) {
alert(data.status);
}
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19449 次 |
| 最近记录: |