2013-12-11 20:38
浏览 142

在javascript .send函数中传递数组

This is regarding passing an array to a php page.Is it possible to send an array like this(code below)? if its not possible , what changes should i bring about to my code?

function ajax_post(){    
var hr = new XMLHttpRequest();"POST", "get_numbers.php", true);  
hr.setRequestHeader("Content-type", "application/json");    
hr.onreadystatechange = function() {
    if(hr.readyState == 4 && hr.status == 200) {
        var data = JSON.parse(hr.responseText);
        var results=document.getElementById("ace");
        for (var obj in data)
var cards= new Array();
results.innerHTML = "processing...";
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dounao1856 2013-12-11 20:46

    .send() doesn't take a javascript array. There are a number of forms of data you could send and you will have to decide which form is appropriate, but an array is not one of them. The simplest would be to turn the array into a JSON string and send that.

    var cards = [

    Then, on the receiving end of things, you would parse the JSON back into whatever language form you are using on the server end. If it's PHP, then you can use the PHP functions for parsing the JSON which will put the data into a PHP array on your server.

    Per the MDN doc page for the XMLHttpRequest object, .send() can take the following types of data:

    void send();
    void send(ArrayBufferView data);
    void send(Blob data);
    void send(Document data);
    void send(DOMString? data);
    void send(FormData data);

    Using JSON would be using the string type.

    打赏 评论

相关推荐 更多相似问题