使用纯js在AJAX请求上传递数组? [英] Pass array on AJAX request using plain js?

查看:621
本文介绍了使用纯js在AJAX请求上传递数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery在POST请求上传递数组,但是我不知道如何使用香草" javascript做同样的事情.这是我的jQuery:

I'm passing an array on a POST request using jQuery, however I don't understand how to do the same with "vanilla" javascript. This is my 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的方式:

This is currently how I'm trying to do it with plain 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']);

这篇关于使用纯js在AJAX请求上传递数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆