不能使用Jquery.post()发布JSON对象

时间:2022-12-01 15:49:35

I have the following object that gets created in my javascript application.

我在javascript应用程序中创建了以下对象。

    poll_data[active_question] = {
        'question': $('div.question_wrap textarea').attr('value'),
        'answers':  [
            $('div.answer_wrap input#0').attr('value'),
            $('div.answer_wrap input#1').attr('value'),
            $('div.answer_wrap input#2').attr('value'),
            $('div.answer_wrap input#3').attr('value'),
            $('div.answer_wrap input#4').attr('value'),
            $('div.answer_wrap input#5').attr('value')
        ]
    };

active_question is set to 'poll', 0, 1, 2, 3, 4, or 5 depending on the question being worked on at the moment. I am trying to post this object to a php script using the following JS code.

active_question设置为“poll”,0、1、2、3、4或5,具体取决于当前正在处理的问题。我正在尝试使用以下JS代码将这个对象发布到php脚本中。

    $.ajax({
        url:            '/somewebsite/poll/create?json=show',
        type:           'POST',
        // dataType:        'json',
        data:           poll_data,
        contentType:        'application/json; charset=utf-8',
        success:        function(data) {
            alert(data);
        }
    });

My PHP code is very simple.

我的PHP代码非常简单。

    echo json_encode($_POST); exit;

When I run the script and click the button that triggers the submission of the data, I receive the alert (so the actual ajax code works), but the result from my PHP script is just an empty array. I think that this is an issue with the way the object is constructed, but I am not sure, and have not been able to find a work around.

当我运行脚本并单击触发数据提交的按钮时,我接收到警报(因此实际的ajax代码工作),但是我的PHP脚本的结果只是一个空数组。我认为这是一个关于物体构造方式的问题,但我不确定,也没能找到一个围绕的工作。

Thanks in advance.

提前谢谢。

2 个解决方案

#1


1  

Okay, a few things:

好的,几件事:

poll_data is not a valid JSON object. You would have to use poll_data[active_question], which IS a valid JSON object. jQuery should serialize this correctly. Remove the contentType -- I am pretty sure that is for php (not positive) but your code wouldn't work for me until I removed it. Finally, the appending of json=show to the query string doesn't do anything..it will just be ignored.

poll_data不是一个有效的JSON对象。您必须使用poll_data[active_question],这是一个有效的JSON对象。jQuery应该正确地序列化它。删除contentType——我很确定这是针对php的(不是肯定的),但是您的代码在我删除之前不会对我起作用。最后,将json=show附加到查询字符串后没有任何作用。它只会被忽略。

A couple minor things too: you can use .val() instead of .attr('value'), and have you looked into .serialize() to create your post data for you?

还有一些小事情:您可以使用.val()而不是.attr('value'),并且您是否研究过.serialize()来为您创建post数据?

#2


0  

do this on server

这样做在服务器

$data;
$data->question=$_POST['question']
$data->answer=$_POST['answers']
echo json_encode($data);

do this for ajax request

这样做是为了ajax请求吗

 $.ajax({
        url: '/somewebsite/poll/create?json=show',
        type:'POST',
        //modified data proprty
        data:poll_data[active_question],
        success: function(data) {
            alert(data);
        }
    });

#1


1  

Okay, a few things:

好的,几件事:

poll_data is not a valid JSON object. You would have to use poll_data[active_question], which IS a valid JSON object. jQuery should serialize this correctly. Remove the contentType -- I am pretty sure that is for php (not positive) but your code wouldn't work for me until I removed it. Finally, the appending of json=show to the query string doesn't do anything..it will just be ignored.

poll_data不是一个有效的JSON对象。您必须使用poll_data[active_question],这是一个有效的JSON对象。jQuery应该正确地序列化它。删除contentType——我很确定这是针对php的(不是肯定的),但是您的代码在我删除之前不会对我起作用。最后,将json=show附加到查询字符串后没有任何作用。它只会被忽略。

A couple minor things too: you can use .val() instead of .attr('value'), and have you looked into .serialize() to create your post data for you?

还有一些小事情:您可以使用.val()而不是.attr('value'),并且您是否研究过.serialize()来为您创建post数据?

#2


0  

do this on server

这样做在服务器

$data;
$data->question=$_POST['question']
$data->answer=$_POST['answers']
echo json_encode($data);

do this for ajax request

这样做是为了ajax请求吗

 $.ajax({
        url: '/somewebsite/poll/create?json=show',
        type:'POST',
        //modified data proprty
        data:poll_data[active_question],
        success: function(data) {
            alert(data);
        }
    });