jQuery ajax对象数组php [英] jquery ajax object array php

查看:67
本文介绍了jQuery ajax对象数组php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

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) {

如果我查看通过chromes inspect工具通过标题提交的表单数据,则会显示:

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

在PHP中,我使用

$sent_data = file_get_contents('php://input');
$sent_data_decoded = json_decode($sent_data, true);

php接收的字符串是

the string that php receives is

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

我需要对数据做些什么,以使其与值一起传递到php中?

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

谢谢.

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

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

如果我使用

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

这是发送的邮件

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

从PHP使用json_decode(file_get_contents('php://input'), true);

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

当我对数据进行print_r时,它是一个空数组吗?

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

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

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

将jquery帖子更新为

Updated the jquery post to

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

更新了php接收代码,以处理以旧方式发送数据的新方式

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);
            }

由于某种原因,如果我不将JSON.stringify(apiQuizData)分配给另一个对象的值,它将无法正常工作.浏览器本身似乎使文本感到窒息,我想是因为它本身是一个巨大的文本字符串?没有把握.无论哪种方式,以上更新#2都解决了我遇到的问题.

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.

谢谢.

推荐答案

将对象字符串化为JSON字符串:

Stringify the object into a JSON string:

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

这篇关于jQuery ajax对象数组php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆