jQuery中我可以做一個數組這發送使用javascript AJAX
myAray=['abc', '123', 'more'];
$.post('myscript.php', {data:myAray}, function(data){
alert(data);
});
我能如何使用普通的JavaScript是一回事嗎?我想使用POST方法將數組發送到我的php腳本。我發現了很多例子,但它們都與jQuery相關。
在此先感謝。
jQuery中我可以做一個數組這發送使用javascript AJAX
myAray=['abc', '123', 'more'];
$.post('myscript.php', {data:myAray}, function(data){
alert(data);
});
我能如何使用普通的JavaScript是一回事嗎?我想使用POST方法將數組發送到我的php腳本。我發現了很多例子,但它們都與jQuery相關。
在此先感謝。
你將不得不使用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);
}
類似這樣的: post是POST或GET。 參數僅用於POST,否則包含您在GET的URL中所需的內容。 成功和錯誤是功能都字符串名稱,而不是函數本身,這就是爲什麼你需要executeFunctionByName,感謝傑森鵐: How to execute a JavaScript function when I have its name as a string
getRemoteData = function (url, post,params, success, error){
var http = false;
if (navigator.appName === "Microsoft Internet Explorer") {
http = new ActiveXObject("Microsoft.XMLHTTP");
}
else {
http = new XMLHttpRequest();
}
http.open(post, url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function() {var resp; if (http.readyState === 4 && http.status == 200) { resp=http.responseText; executeFunctionByName(success, window, resp); }else if(http.status == 400){resp=http.responseText; executeFunctionByName(error, window, resp);}};
http.send(params);
return false;
};
function executeFunctionByName(functionName, context, args) {
args = Array.prototype.slice.call(arguments).splice(2);
var namespaces = functionName.split(".");
var func = namespaces.pop();
for(var i = 0; i < namespaces.length; i++) {
context = context[namespaces[i]];
}
return context[func].apply(this, args);
}
陷入混亂與此有關。
JS
var myarray = Array("test","boom","monkey");
send("test.php", myarray);
function send(url, data)
{
var xhr = new XMLHttpRequest();
xhr.onreadystatechange=function()
{
if (xhr.readyState==4 && xhr.status==200)
{
console.log(xhr.responseText);
}
}
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send("data= " +data);
}
PHP
<?php
$array = explode(',', $_POST["data"]);
for($i=0,$l=count($array); $i<$l; $i++)
{
echo $array[$i].'<br>';
}
?>
這將是很高興知道爲什麼我的答案被投票。我們都在這裏學習! – manish
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");
}
就拿注意這裏: test [] = Henry & test [] = Ford「
其中test是您在php中使用的數組的名稱。
在PHP
<?php
print_r($_POST['test']);
?>
它會產生:陣列([0] =>亨利[1] =>福特)
有噸教程此的。檢查谷歌與「AJAX後JavaScript數組」 – Chris
第四個答案從上到下: [http://stackoverflow.com/questions/5350377/how-to-make-an-ajax-request-to-post- JSON-數據和過程的響應] [1] [1]:http://stackoverflow.com/questions/5350377/how-to-make-an-ajax-request-to -post-json-data-and-process-the-response –