最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Jquery Ajax POST request failing - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited May 9, 2013 at 10:01 ragebunny asked May 9, 2013 at 9:34 ragebunnyragebunny 1,77010 gold badges37 silver badges55 bronze badges 19
  • Is signals.php expecting json? Did you try sending it to PHP as: data:{markets:post_data}, You could also add failure: function(){} to the Ajax. – Carol Skelly Commented May 9, 2013 at 9:37
  • @AlbertoZaccagni Yes, i had this working with GET instead of post before this. Also I'm getting a return from the PHP file so we know it's the right URL. – ragebunny Commented May 9, 2013 at 9:38
  • @DevalShah Hey, i just trying the print_r and just got an empty array. – ragebunny Commented May 9, 2013 at 9:40
  • You're expecting a JSON result returned, so anything else will fail. Try adding on a fail() function to see what the problem is. – adeneo Commented May 9, 2013 at 9:42
  • 1 Nope, 1.8.3 supports done() and fail() just fine, and the posted fiddle returns the errors just fine to the console, so you should'nt be getting those errors in that version of jQuery. You're sure you don't have two jQuery versions included. – adeneo Commented May 9, 2013 at 10:18
 |  Show 14 more ments

3 Answers 3

Reset to default 3

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

发布评论

评论列表(0)

  1. 暂无评论