dourao3960 2017-08-23 12:07
浏览 147
已采纳

Ajax - 使用jQuery发送对象数组

I am constructing an array of objects from form data in order to send to a processing script that will then send the data on to an API. The API does not allow CORS ajax requests, hence the need to send to an external script first.

I can return simple strings, but when I stringify the data I have, the response from the server is always that the POST or GET (have tried both) data is just an empty array.

JS

$('#enrol').submit(function(e) {
    e.preventDefault();

    var collection = [];

    $('#enrol .form-row').each(function() {
        var email = $(this).find('input[type="email"]').val();
        var c1Val = $(this).find('.c1').is(':checked') ? 'true' : 'false';
        var c2Val = $(this).find('.c2').is(':checked') ? 'true' : 'false';
        var c3Val = $(this).find('.c3').is(':checked') ? 'true' : 'false';
        var c4Val = $(this).find('.c4').is(':checked') ? 'true' : 'false';
        var c5Val = $(this).find('.c5').is(':checked') ? 'true' : 'false';
        var c6Val = $(this).find('.c6').is(':checked') ? 'true' : 'false';
        var c7Val = $(this).find('.c7').is(':checked') ? 'true' : 'false';

        var person = {
            'email'   : email,
            'course1' : c1Val,
            'course2' : c2Val,
            'course3' : c3Val,
            'course4' : c4Val,
            'course5' : c5Val,
            'course6' : c6Val,
            'course7' : c7Val,
        }
        collection.push(person);

    });

    var dataString = JSON.stringify(collection);

    $.ajax({
        url: 'http://www.example.com/script.php',
        data: dataString,
        crossDomain: true,
        success: function(response) {
            console.log(response);
        },
        error: function(response) {
            console.log(response);
            alert(response.responseText);
        }
    });
})

PHP

header("Access-Control-Allow-Origin: URLHERE");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");

var_dump($_GET);

What I don't get is if I just JSON.stringify a simple string, I get the data returned, yet the array of objects seems to arrive at the php script as an empty array - or at least that's what the console.log is suggesting. I've outputted the dataString variable to ensure it has data - it does.

  • 写回答

3条回答 默认 最新

  • dourao1896 2017-08-24 11:00
    关注

    The solution was a simple one, I wasn't providing a key for the post array -

    data: dataString
    

    should have been

    data: { data: dataString }
    

    Then the output of var_dump($_GET['data')) contained the data I was sending.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 MDK–ARM里一直找不到调试器
  • ¥15 oracle中sql查询问题
  • ¥15 vue使用gojs3.0版本,在nodeDataArray中的iconSrc使用gif本地路径,展示出来后动画是静态的,不是动态的
  • ¥100 代写个MATLAB代码,有偿
  • ¥15 ansys electronics 2021 R1安装报错,错误代码2,如图
  • ¥15 Dev-c++打字不出现中文,但出现日文
  • ¥30 搭建面包板由NE555N和SN74LS90N组成的计时电路时出了问题
  • ¥15 无源定位系统的时差估计误差标准差
  • ¥15 请问这个代码哪里有问题啊
  • ¥20 python--version在命令端输入结果Python is not defined怎么办?还有pip不是exe格式是不是没安装成功?