javascript - 使用普通 js 在 AJAX 请求上传递数组?

标签 javascript jquery ajax

我正在使用 jQuery 在 POST 请求上传递一个数组,但是我不明白如何使用“vanilla”javascript 执行相同的操作。这是我的 jQuery:

    // Request using jQuery
$.ajax({type:'POST',url:'insert-user.php',data: {myArray: myArray},
success:function(data_response){
    console.log("jQuery, got data back, response: "+data_response);
}});

这就是我目前尝试使用纯 js 实现的方式:

// Request using plain js
var xmlhttp;
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
} else {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200){
        console.log("JS, got data back, response: "+xmlhttp.responseText);
    }
}
xmlhttp.open("POST","insert-user.php",true);
xmlhttp.send({myArray: myArray});

最佳答案

使用这个:

xmlhttp.send('myArray='+JSON.stringify(myArray));

在 php 端,

$array= json_decode($_POST['myArray']);

关于javascript - 使用普通 js 在 AJAX 请求上传递数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19429286/

相关文章:

javascript - Karma 测试 Kendo UI 的视觉响应

javascript - 在 Ember 中获取表单内所有值的哈希值

Jquery Ajax 加载图像

javascript - ng-table 与用户输入的过滤器一起使用

javascript - 使用具有混合 namespace 的 E4X 获取第一个节点?

c# - 将值从代码隐藏传递到 Javascript

java - Struts2 tabedpanel 标签不工作

javascript - 将 mp3 片段从、到并应用淡入、淡出

javascript - 嵌套 html 元素中的 onClick 事件

javascript - 在 IE7 和 IE8 (jQuery) 上创建内容失败的 <noscript> 元素