如何使用XMLHttpRequest传递数组值

时间:2016-08-07 15:09:09

标签: javascript jquery arrays xmlhttprequest

我需要使用XMLHttpRequest

发送数组参数或值

像这样的东西

$(document).on('click','.save_edit_class',function(){

var crit_name_text = $('#crit_name_text').val();
var crit_desc = $('#crit_desc_text').val();
var sub_crit_arr = [];
$('.sub_crit_text').each(function(k , v){
    sub_crit_arr[k] = $(v).val();

});

var http = new XMLHttpRequest();
var url = "xml_remove_criteria.php";
var params =  "crit_name_text="+crit_name_text+"&crit_desc="+crit_desc+"&sub_crit_arr[]="+sub_crit_arr+"";

http.open("GET", url+"?"+params, true);
http.onreadystatechange = function(){//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200){
        alert(http.responseText);
    }
}
http.send(null);

});

如何将sub_crit_arr(数组)变量作为数组发送到我的xml页面?我试图使用它发送,但我得到了一个错误无效的参数提供foreach。

这是我的xml页面..

<?php
session_start();
include("../global.php");
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';

echo '<response>';
    if(isset($_GET['crit_name_text'])){
       $criteria_name = $_GET['crit_name_text'];
       echo $criteria_name;//testing

        if(isset($_GET['crit_desc'])){
            $crit_desc = $_GET['crit_desc'];
            echo $crit_desc;//testing
        }


    if(isset($_GET['sub_crit_arr'])){
        $sub_crit_arr = $_GET['sub_crit_arr'];
        $sub_crit_arrs = array();
        $count = 0;
        foreach ($sub_crit_arr as $value) {
            $sub_crit_arr[$count] = $value;
            echo $sub_crit_arr[$count]." - value<br/>";//testing
            $count++;
        }
    }

   }
   echo '</response>';

&GT;

0 个答案:

没有答案