AJAX callback warning not working?

I checked 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 problem. Basically, I make an application for microblogging and insert a tweet when a button is pressed that calls the jQuery ajax function. Here's the corresponding code:

home.js

This is a jQuery ajax 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);
    }
})
}

The Ajax call works successfully, and the tweet is inserted, but I cannot get a warning call in fireback under the success parameter. I tried something like alert('abc');, but that didn't work either.

tweet.php

This is just a shell, it 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);
?>

It just inserts a tweet into the database, and I wanted to try sending simple formatted JSON data, but data.statusdidn't work on a success callback.

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

success complete, . data :

data.status 200

JSON data['success'] PHP 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 shot

console error

+4
4

:

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

. , 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 document

: ,

0

PHP:

header( 'Content-Type: application/json' );

:

echo json_encode( $data );

script:

exit;

, !

0
source

try changing the input type from "send" to "button". I hope this works.

0
source

Source: https://habr.com/ru/post/1545630/


All Articles