Ⅰ js到底如何將一堆數組通過post的方法發送給php
如果有其他參數可以將數組轉換為json字元後,再將轉換的結果賦值給一個參數,內通過Ajax的post提交容給PHP
序列化可以用jquery.serialize();
json:JSON.stringify(arr).toString();
Ⅱ JS加密不可靠,用post方法行嗎
如果那段代碼是用來處理數據可以用ajax來實現。
我簡單舉個例子比如這個函數
functioncount(a){
returna+=10;
}
參數 a 返回值+10,但是我們不想讓別人知道函數內部是怎麼處理的
我們可以把處理的過程移到後台,這里我後台用php舉例 test1.php
<?php
$num=$_POST['num'];//取前台的參數值
$num+=10;//計算
echo$num;//返回
?>
然後在前端用ajax傳參數到後台,得到計算的返回值,我這里用jquery的post
<scriptsrc="http://code.jquery.com/jquery-latest.js"></script>
<script>
window.onload=function(){
varnum=2;
alert(count(num));
$.post('test1.php',{num:num},function(s){
alert(s)
})
}
functioncount(a){
returna+=10;
}
</script>
運行這段代碼,就可發現alert(count(num)); 與post回傳的alert(s)值是一樣的,但是用post別人是看不到 += 10這段後台處理過程的
原理就是這么簡單
Ⅲ js 模擬POST提交enctype="multipart/form-data"類型的表單
只是需要文件上傳才用它的
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
改成
xmlHttp.setRequestHeader("Content-Type","multipart/form-data;")。
Ⅳ js像這樣發送json到php,為什麼$_POST為空如何做
首先,盡可能利用jquery的ajax來發送數據,這樣它會自動把你的json編碼成x-www-form-urlencode的格式,這樣php的$_POST就能獲得數據了。
如果一定要用原生js來做ajax,那就要自己來實現編碼和解碼,比如這樣:
httpRequest=newXMLHttpRequest();
httpRequest.open("POST",$url,true);
httpRequest.setRequestHeader("Content-Type","application/json");
vararr=["124","424","aab","cdf","哈哈哈"];
varstr=JSON.stringify(arr);
httpRequest.send(str);
PHP端:
$data=file_get_contents('php://input');
$arr=json_decode($data,true);
print_r($arr);
Ⅳ JS發送json格式POST請求有哪些方式
以Ajax方式發送
<scripttype="text/javascript">
一、獲取url所有參數值
functionUS(){
varname,value;
varstr=location.href;
varnum=str.indexOf("?");
str=str.substr(num+1);
vararr=str.split("&");
for(vari=0;i<arr.length;i++){
num=arr[i].indexOf("=");
if(num>0){
name=arr[i].substring(0,num);
value=arr[i].substr(num+1);
this[name]=value;
}
}
}
二、使用JS發送JSON格式的POST請求
varus=newUS();
varxhr=newXMLHttpRequest();
xhr.open("POST","/searchguard/api/v1/auth/login",true);
xhr.setRequestHeader("Content-type","application/json");
xhr.setRequestHeader("kbn-version","5.3.0");
xhr.onreadystatechange=function(){
if(xhr.readyState==4){
if(xhr.status==200){
window.location.href=us.nextUrl;
}
}
};
xhr.send(JSON.stringify({
"username":us.u,
"password":us.p
}));
</script>
Ⅵ 怎麼用javascript發送post請求
第一種提交post的方式是傳統方式,判斷瀏覽器進行post請求。
<SCRIPT stype=text/javascript>
var xmlobj; //定義XMLHttpRequest對象
function CreateXMLHttpRequest()
{
if(window.ActiveXObject)
//如果當前瀏覽器支持Active Xobject,則創建t對象
{
//xmlobj = new ActiveXObject("Microsoft.XMLHTTP");
try {
xmlobj = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlobj = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlobj = false;
}
}
}
else if(window.XMLHttpRequest)
//如果當前瀏覽器支持XMLHttp Request,則創建XMLHttpRequest對象
{
xmlobj = new XMLHttpRequest();
}
}
function SubmitArticle(act,cityname,antique) //主程序函數
{
CreateXMLHttpRequest(); //創建對象
//var parm = "act=firstweather" ;//構造URL參數
//antique = escape(antique);
var parm = "act=" + act + "&cityname=" + cityname + "&antique=" + antique;//構造URL參數
//xmlobj.open("POST", "{dede:global.cfg_templeturl/}/../include/weather.php", true); //調用weather.php
xmlobj.open("POST", "/weather/include/weather.php", true); //調用weather.php
xmlobj.setRequestHeader("cache-control","no-cache");
xmlobj.setRequestHeader("contentType","text/html;charset=uft-8") //指定發送的編碼
xmlobj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;"); //設置請求頭信息
xmlobj.onreadystatechange = StatHandler; //判斷URL調用的狀態值並處理
xmlobj.send(parm); //設置為發送給伺服器數據
}
第二種方式則是虛擬表單的形式提交post請求
function post(URL, PARAMS) {
var temp = document.createElement("form");
temp.action = URL;
temp.method = "post";
temp.style.display = "none";
for (var x in PARAMS) {
var opt = document.createElement("textarea");
opt.name = x;
opt.value = PARAMS[x];
// alert(opt.name)
temp.appendChild(opt);
}
document.body.appendChild(temp);
temp.submit();
return temp;
}
調用方法 如:
復制代碼 代碼如下:
post('pages/statisticsJsp/excel.action', {html :prnhtml,cm1:'sdsddsd',cm2:'haha'});
Ⅶ jsp頁面使用js或者jQuery 向一個地址Post提交表單,參數LinkId ..獲取到這個地址返回的信息...求代碼
建議你使用jQuery form 提交表單 提交的地址 提交方法在form定義
需要引入
jQuery.min.js
jQuery.form.js
jQuery.ajaxSubmit({
dataType:'json',//返回的信息 大量數據使用json方便 如果只是幾個字元 使用text
success:function(data){
//你處理信息的方法
}
});
使用的是AJAX
如果你要傳的值用get就能滿足的話 jQuery.ajax也可以使用 而且方便