Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript-ajax发送数组_Javascript_Ajax - Fatal编程技术网

使用javascript-ajax发送数组

使用javascript-ajax发送数组,javascript,ajax,Javascript,Ajax,在jquery中,我可以做到这一点 myAray=['abc', '123', 'more']; $.post('myscript.php', {data:myAray}, function(data){ alert(data); }); 我如何使用普通javascript做同样的事情?我想使用POST方法向我的php脚本发送一个数组。我发现了很多例子,但都与jquery相关 提前谢谢 类似这样: post是post或GET。 参数仅在POST中使用,否则会在url中包含GET所需的内

在jquery中,我可以做到这一点

myAray=['abc', '123', 'more'];
$.post('myscript.php', {data:myAray}, function(data){
    alert(data);
});
我如何使用普通javascript做同样的事情?我想使用POST方法向我的php脚本发送一个数组。我发现了很多例子,但都与jquery相关

提前谢谢

类似这样: post是post或GET。 参数仅在POST中使用,否则会在url中包含GET所需的内容。 success和error都是函数的字符串名,而不是函数本身,这就是为什么需要executeFunctionByName的原因,多亏了Jason Bunting:

getRemoteData=function(url、post、参数、成功、错误){
var-http=false;
如果(navigator.appName==“Microsoft Internet Explorer”){
http=新的ActiveXObject(“Microsoft.XMLHTTP”);
}
否则{
http=newXMLHttpRequest();
}
http.open(post、url、true);
http.setRequestHeader(“内容类型”、“应用程序/x-www-form-urlencoded”);
http.onreadystatechange=function(){var resp;if(http.readyState==4&&http.status==200){resp=http.responseText;executeFunctionByName(success,window,resp);}否则if(http.status==400){resp=http.responseText;executeFunctionByName(error,window,resp);};
http.send(params);
返回false;
};
函数executeFunctionByName(函数名、上下文、参数){
args=Array.prototype.slice.call(参数).splice(2);
var namespace=functionName.split(“.”);
var func=namespaces.pop();
for(var i=0;i
把这件事弄得一团糟

JS

PHP


您必须使用
XMLHttpRequest
并自己序列化数组:

function ajax(myArray) {

    var xmlHTTP;

    if (window.XMLHttpRequest) { 
        xmlHTTP = new XMLHttpRequest();
    } else { 
        xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlHTTP.onreadystatechange = function () {
        if (xmlHTTP.readyState == 4 && xmlHTTP.status == 200) {
            // do whatever it is you want to do
        }
    }

    //Serialize the data
    var queryString = "";
    for(var i = 0; i < myArray.length; i++) {
        queryString += "myArray=" + myArray[i];

        //Append an & except after the last element
        if(i < myArray.length - 1) {
           queryString += "&";
        }
    }

    xmlHTTP.open("POST", "www.myurl.com", true);
    xmlHTTP.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
    xmlHTTP.send(queryString);
}
函数ajax(myArray){
var-xmlHTTP;
如果(window.XMLHttpRequest){
xmlHTTP=新的XMLHttpRequest();
}否则{
xmlHTTP=新的ActiveXObject(“Microsoft.xmlHTTP”);
}
xmlHTTP.onreadystatechange=函数(){
if(xmlHTTP.readyState==4&&xmlHTTP.status==200){
//做你想做的事
}
}
//序列化数据
var queryString=“”;
对于(var i=0;i
请注意: test[]=Henry&test[]=Ford“

其中test是将在php中使用的数组的名称

在php中

<?php
 print_r($_POST['test']);
?>


它会产生:Array([0]=>Henry[1]=>Ford)

有很多关于这个的教程。用“ajax post javascript Array”自上而下的第四个答案检查google:[[1]:我能在$\u post['data']中得到这个吗?我想它会显示在
$\u post['myArray']
下。你可以将for循环中的字符串更改为
“data”=“+myArray[i];
并且它应该显示在
$\u POST['data'下]
什么是
查询字符串
这里?我认为应该是
数据
@user2567857:不要对帖子进行修改,这样会改变原来的意思。一条评论就足够了。@staticx,这取决于OP。这个答案被接受,因此用户可能不会阅读评论。很高兴知道我的答案为什么会被否决d、 我们都在这里学习!
<?php 
$array = explode(',', $_POST["data"]);

for($i=0,$l=count($array); $i<$l; $i++) 
{
echo $array[$i].'<br>';
}
?>
function ajax(myArray) {

    var xmlHTTP;

    if (window.XMLHttpRequest) { 
        xmlHTTP = new XMLHttpRequest();
    } else { 
        xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlHTTP.onreadystatechange = function () {
        if (xmlHTTP.readyState == 4 && xmlHTTP.status == 200) {
            // do whatever it is you want to do
        }
    }

    //Serialize the data
    var queryString = "";
    for(var i = 0; i < myArray.length; i++) {
        queryString += "myArray=" + myArray[i];

        //Append an & except after the last element
        if(i < myArray.length - 1) {
           queryString += "&";
        }
    }

    xmlHTTP.open("POST", "www.myurl.com", true);
    xmlHTTP.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
    xmlHTTP.send(queryString);
}
function loadXMLDoc()
{
  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)
   {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
   }
  }
xmlhttp.open("POST","jsArrayPhp.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("test[]=Henry&test[]=Ford");
}
<?php
 print_r($_POST['test']);
?>