javascript - AJAX success callback alert not working? -
i've checked out various other posts on so, don't seem see problem, , hoping if help me shed lite on issue. basically, i'm doing microblogging appliation , inserting tweet when button clicked, calls jquery ajax function. here's respective code:
home.js
this 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], // careful of @! - @username tweet : tweet_text, date : gettimenow() }; $.ajax({ type : 'post', url : '../php/tweet.php', info : postdata, datatype : 'json', success : function(data) { alert(data.status); } }) }
the ajax phone call works successfully, , tweet inserted, can't alert phone call fireback under success parameter. tried basic alert('abc');
didn't work either.
tweet.php
this wrapper, looks 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); ?>
this inserts tweet database, , wanted seek sending simple json formatted info back, data.status
didn't work on success callback.
db_functions.php
this insert_tweet function in, , looks this:
function insert_tweet($author, $tweet, $date) { global $link; $author_id = get_user_id($author); $query = "insert tweets (`author id`, `tweet`, `date`) values ('{$author_id}', '{$tweet}', '{$date}')"; $result = mysqli_query($link, $query); }
i've tested it, , i'm pretty sure runs fine. uncertainty cause of problem, if is, i'm ears. i've tested $link
, defined in file included in top of db_functions.php
file, , works.
would appreciate advice regarding this, thanks!
update
changed success
complete
, , works. however, data
object seems bit odd:
data.status
pops 200 in alert
i tried changing json array element name data['success']
in php, , accessed in front end end data.success
, , outputted in alert box:
function () { if ( list ) { // first, save 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 ); // need add together callbacks // current firing batch? if ( firing ) { firinglength = list.length; // memory, if we're not firing // should phone call right away } else if ( memory ) { firingstart = start;…
what mean??
update 2
okay, don't know if helps, i've printed console log chrome's inspector, , if i'm not mistaken, json info sent fine. here's 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] } __proto__: function __proto__() { [native code] } set __proto__: function __proto__() { [native code] }
update 3
console error scrn shot
try this:
$.ajax({ type : 'post', url : '../php/tweet.php', info : postdata, datatype : 'json', finish : function(data) { alert(data.status); } })
javascript php jquery ajax twitter
No comments:
Post a Comment