简体   繁体   中英

Jquery Ajax POST request failing

could someone tell me why this request doesn't execute the success code?

$(document).ready(function(){
        var post_data = [];
        $('.trade_window').load('signals.php?action=init'); 
        setInterval(function(){
            post_data = [   {market_number:1, name:$('.trade_window .market_name_1').text().trim()},
                    {market_number:2, name:$('.trade_window .market_name_2').text().trim()}];

                    console.log(JSON.stringify({markets: post_data}));

            $.ajax({
                        url: 'signals.php',
                        type: 'POST',
                        contentType: 'application/json; charset=utf-8',
                       data:{markets:post_data},
                        dataType: "json",
                        success: function(){
                            console.log("IT WORKED");
                        },
                        failure: function(result){
                            console.log("FAILED");
                            console.log(result);
                        }
            });
        }, 10000); 
    });

When i check the output of console.log(JSON.stringify({markets: post_data})); is get this as a result in google chrome:

{"markets":[{"market_number":1,"name":"GBPUSD"},{"market_number":2,"name":"EURUSD"}]}

But i never get "IT WORKED" printed to the console which means it never works.

on futher checking i made an if statement in my php which checked if anything was posted

if(!empty($_POST))
        echo "POSTED!!!!!";
    else
        echo "NOT POSTED";

But i always get "NOT POSTED" printed on the screen.

any ideas?

Thanks for the help.

use this code:

$(document).ready(function(){
    var post_data = [];
    $('.trade_window').load('signals.php?action=init'); 
    setInterval(function(){
        post_data = [   {market_number:1, name:$('.trade_window .market_name_1').text().trim()},
                {market_number:2, name:$('.trade_window .market_name_2').text().trim()}];

                console.log(JSON.stringify({markets: post_data}));

        $.ajax({
                    url: 'signals.php/',
                    type: 'POST',
                    contentType: 'application/json; charset=utf-8',
                   data:{markets:post_data},
                    dataType: "json",
                    success: function(){
                        console.log("IT WORKED");
                    },
                    failure: function(result){
                        console.log("FAILED");
                        console.log(result);
                    }
        });
    }, 10000); 
});

You can easily pass POST data in ajax request without JSON.stringify .

$(document).ready(function(){
        var post_data = [];
        $('.trade_window').load('signals.php?action=init'); 
        setInterval(function(){
            post_data = '&market_number1=1&name1='+$(".trade_window .market_name_1").text().trim()+'&market_number2=2&name2='+$(".trade_window .market_name_2").text().trim();

            $.ajax({


                        url: 'signals.php',
                        type: 'POST',
                        data: post_data,
                        dataType: "json",
                        success: function(){
                            console.log("IT WORKED");
                        }
            });
        }, 2000); 
    });

if you want to use json.stringyfy and read the data on php side.. this is the right way to do it

data: {json: JSON.stringify({markets: post_data})}

$(document).ready(function(){
    var post_data = [];
    //$('.trade_window').load('signals.php?action=init'); 
    setInterval(function(){
        post_data = [   {market_number:1, name:$('.trade_window .market_name_1').text().trim()},
                {market_number:2, name:$('.trade_window .market_name_2').text().trim()},];

                console.log(JSON.stringify({markets: post_data}));

        $.ajax({


                    url: 'signals.php',
                    type: 'POST',
                    data: {json: JSON.stringify({markets: post_data})},
                    dataType: "json",
                    done: function($msg){
                        console.log("IT WORKED");
                    }
        });
    }, 2000); 
});

now in your PHP you can do whatever

$json = json_decode($_POST["json"]);
print_r($json);

or

    if(isset($_POST["json"])){
$json = json_decode($_POST["json"]);
if(!empty($json))
        echo "POSTED!!!!!";
    else
        echo "NOT POSTED";
}

DIns

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM