Jquery ajax POST响应为空

时间:2022-10-08 21:30:15

I have a js script that does an ajax request and posts the data to a php script, this script with then echo something back depending if it works or not.

我有一个执行ajax请求的js脚本并将数据发布到php脚本,这个脚本然后根据它是否有效回显一些东西。

here is the JS

这是JS

$(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()}];

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

here is the php:

这是php:

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

So basically, i thought the response in the `success: function(response)' method would be populated with either "IT WORKED!!!" or "NOT POSTED" depending on the if statement in the php. Now everything seem to work because the js script manages to go into the success statement but prints this to the console:

所以基本上,我认为“成功:功能(响应)”方法中的响应将填充“IT WORKED !!!”或“NOT POSTED”取决于php中的if语句。现在一切似乎都有效,因为js脚本设法进入成功语句,但将其打印到控制台:

Response was null

回复无效

I need to be able to get the return from the server in order to update the screen.

我需要能够从服务器获得返回以更新屏幕。

Any ideas what I'm doing wrong?

我有什么想法我做错了吗?

7 个解决方案

#1


4  

Try:

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

#2


1  

use this in your php file

在你的php文件中使用它

if(isset($_POST["markets"]))
    {

    }

instead of

if(isset($_POST["json"]))
    {
.
.
.
.
}

#3


0  

Obiously the if(isset($_POST["json"])) statement is not invoked, so neither of both echos is executed. The fact that the function specified in .ajax success is invoked, only tells you that the http connection to the url was successful, it does not indicate successful processing of the data.

很明显没有调用if(isset($ _ POST [“json”]))语句,因此两个回声都没有被执行。调用.ajax成功中指定的函数的事实,仅告诉您与url的http连接成功,它不表示成功处理数据。

#4


0  

You are using "success:" wrong.

你使用“成功:”错了。

Try this instead.

试试这个。

$.post("signals.php", { markets: post_data }).done(function(data) {
    /* This will return either "IT WORKED!!!!" or "NOT POSTED" */
    alert("The response is: " + data);
});

Also have a look at the jQuery documentation.

另请参阅jQuery文档。

http://api.jquery.com/jQuery.post/

#5


0  

Look, You send data in market variable not in json. Please change on single.php code by this.

看,你在市场变量中发送的数据不是在json中。请更改single.php代码。

$json_data = array();
if(isset($_POST["markets"]))
{
 //  $json = json_decode($_POST["markets"]);
 $json = ($_POST["markets"]);
 if(!empty($json))
        echo "IT WORKED!!!!";
    else
        echo  "NOT POSTED";
}

And change on your ajax function

并改变你的ajax功能

$(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()}];

    $.ajax({
                url: 'signals.php',
                type: 'post',
         //       contentType: 'application/json; charset=utf-8',
                data:{markets:post_data},
                dataType: "json",
                success: function(response){
                    console.log("Response was "  + response);
                },
                failure: function(result){
                    console.log("FAILED");
                    console.log(result);
                }
    });
},6000); 

});

#6


0  

You have to you change you $.ajax call with

你必须改变你的$ .ajax电话

   //below post_data array require quotes for keys like 'market_number' and update with your required data
   post_data = [   {'market_number':1, 'name':'name1'},
            {'market_number':2, 'name':'name2'}];
        //console.log(post_data);
        $.ajax({
            url: "yourfile.php",
            type:'post',
            async: true,
            data:{'markets':post_data},
            dataType:'json',
            success: function(data){
                console.log(data);
            },
        });

and you php file will be

而你的php文件将是

<?php
if(isset($_POST['markets']))
{
    echo "It worked!!!";
}
else
{
    echo "It doesn't worked!!!";
}
//if you want to work with json then below will help you
//$data = json_encode($_POST['markets']);
//print_r($data);
?>

#7


0  

in your php file check the $_POST:

在你的php文件中检查$ _POST:

echo(json_encode($_POST));

which will tell if your data has been posted or not and the data structure in $_POST.

这将告诉你的数据是否已经发布,以及$ _POST中的数据结构。

I have used the following code to covert the posted data to associative array:

我使用以下代码将发布的数据转换为关联数组:

$post_data = json_decode(json_encode($_POST), true); 

#1


4  

Try:

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

#2


1  

use this in your php file

在你的php文件中使用它

if(isset($_POST["markets"]))
    {

    }

instead of

if(isset($_POST["json"]))
    {
.
.
.
.
}

#3


0  

Obiously the if(isset($_POST["json"])) statement is not invoked, so neither of both echos is executed. The fact that the function specified in .ajax success is invoked, only tells you that the http connection to the url was successful, it does not indicate successful processing of the data.

很明显没有调用if(isset($ _ POST [“json”]))语句,因此两个回声都没有被执行。调用.ajax成功中指定的函数的事实,仅告诉您与url的http连接成功,它不表示成功处理数据。

#4


0  

You are using "success:" wrong.

你使用“成功:”错了。

Try this instead.

试试这个。

$.post("signals.php", { markets: post_data }).done(function(data) {
    /* This will return either "IT WORKED!!!!" or "NOT POSTED" */
    alert("The response is: " + data);
});

Also have a look at the jQuery documentation.

另请参阅jQuery文档。

http://api.jquery.com/jQuery.post/

#5


0  

Look, You send data in market variable not in json. Please change on single.php code by this.

看,你在市场变量中发送的数据不是在json中。请更改single.php代码。

$json_data = array();
if(isset($_POST["markets"]))
{
 //  $json = json_decode($_POST["markets"]);
 $json = ($_POST["markets"]);
 if(!empty($json))
        echo "IT WORKED!!!!";
    else
        echo  "NOT POSTED";
}

And change on your ajax function

并改变你的ajax功能

$(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()}];

    $.ajax({
                url: 'signals.php',
                type: 'post',
         //       contentType: 'application/json; charset=utf-8',
                data:{markets:post_data},
                dataType: "json",
                success: function(response){
                    console.log("Response was "  + response);
                },
                failure: function(result){
                    console.log("FAILED");
                    console.log(result);
                }
    });
},6000); 

});

#6


0  

You have to you change you $.ajax call with

你必须改变你的$ .ajax电话

   //below post_data array require quotes for keys like 'market_number' and update with your required data
   post_data = [   {'market_number':1, 'name':'name1'},
            {'market_number':2, 'name':'name2'}];
        //console.log(post_data);
        $.ajax({
            url: "yourfile.php",
            type:'post',
            async: true,
            data:{'markets':post_data},
            dataType:'json',
            success: function(data){
                console.log(data);
            },
        });

and you php file will be

而你的php文件将是

<?php
if(isset($_POST['markets']))
{
    echo "It worked!!!";
}
else
{
    echo "It doesn't worked!!!";
}
//if you want to work with json then below will help you
//$data = json_encode($_POST['markets']);
//print_r($data);
?>

#7


0  

in your php file check the $_POST:

在你的php文件中检查$ _POST:

echo(json_encode($_POST));

which will tell if your data has been posted or not and the data structure in $_POST.

这将告诉你的数据是否已经发布,以及$ _POST中的数据结构。

I have used the following code to covert the posted data to associative array:

我使用以下代码将发布的数据转换为关联数组:

$post_data = json_decode(json_encode($_POST), true);