php得到ajax的object,jquery ajax object array php

I have:

var apiQuizData = {'ect stuff removed...',answers:{}};

$.each(dataCEActiveQuiz.quiz_data, function(index, answer) {

if(answer.selected == undefined){

apiQuizData.answers[answer.id] = 0;

} else {

apiQuizData.answers[answer.id] = answer.selected;

}

});

$.post(URL, apiQuizData, function(data) {

If I look at the form data submitted through the header via chromes inspect tools it shows:

// url decoded

answers[28194]:112768

answers[28195]:112773

answers[28199]:112788

answers[28202]:112803

answers[28204]:112809

// url encoded

answers%5B28194%5D:112768

answers%5B28195%5D:112773

answers%5B28199%5D:112788

answers%5B28202%5D:112803

answers%5B28204%5D:112809

// query string

answers%5B28195%5D=112773&answers%5B28199%5D=112788&answers%5B28202%5D=112803&answers%5B28204%5D=112809

In PHP I use

$sent_data = file_get_contents('php://input');

$sent_data_decoded = json_decode($sent_data, true);

the string that php receives is

&answers=&answers=&answers=&answers=&answers=

What do I need to do to the data so that it goes through to php with the values?

Thanks.

=================

UPDATE 1

If I use

$.post(URL, JSON.stringify(apiQuizData), function(data) {

This is what is sent

{...extra stuff...,"answers":{"28195":"112773","28199":"112791","28201":"112796","28202":"112800","28204":"112810"}}

From PHP using json_decode(file_get_contents('php://input'), true);

{...extrastuff...}id%22%3A952077%2C%22answers%22%3A%7B%2228195%22%3A%22112

When I do a print_r of the data it is an empty array?

=================

UPDATE 2 - Working

Updated the jquery post to

$.post(URL + 'sendCEQuizResults', {jsonstringify: JSON.stringify(apiQuizData)}, function(data) {

Updated the php receiving code to handle the new way I am sending data with the old way

$sent_data = file_get_contents('php://input');

if(substr($sent_data, 0, 13) == 'jsonstringify')

{

parse_str($sent_data);

$sent_data_decoded = json_decode($jsonstringify, true);

} else

{

$sent_data_decoded = json_decode($sent_data, true);

}

For some reason it would not work if I didn't assign the JSON.stringify(apiQuizData) into the value of another object. The browser seemed to choke on the text by itself, I guess because it was a huge text string by itself? not sure. Either way the above update #2 solved the issues I was having.

Thanks.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值