Send array with ajax request to php

前端 未结 5 845
逝去的感伤
逝去的感伤 2021-02-08 03:37

I created array like this [\"9\", \"ques_5\", \"19\", \"ques_4\"]. Now I want to send it from JS to PHP but I\'m not getting proper results. My JS code is:

5条回答
  •  情书的邮戳
    2021-02-08 04:28

    Try this

    var array = ["9", "ques_5", "19", "ques_4"];
    console.log(array.join(","));

    above code will output string with comma separated like 9,ques_5,19,ques_4then paste it to ajax call.

    And then in php explode that string.

    Other possible solutions.

    First

    var obj = { 'item1': 'value1', 'item2': 'value2' };
    
    $.ajax(
    {
        type:  'post', 
        cache:  false ,
        url:  'test/result.php',
        data:  { result : JSON.stringify(obj) },
        success: function(resp)
        {
            alert(resp);
        } 
    });

    Second

    var a = $.JSON.encode(obj);
    
    $.ajax(
    {
        type:  'post', 
        cache:  false ,
        url:  'test/result.php',
        data:  { result : a },
        success: function(resp)
        {
            alert(resp);
        } 
    });
    
    In PHP File
    
    

提交回复
热议问题