首页  >  文章  >  后端开发  >  PHP如何接收javascript POST的数据?

PHP如何接收javascript POST的数据?

WBOY
WBOY原创
2016-06-23 13:54:101146浏览

$(document).ready(function() {    $("#submit").click(function(){		username = 1233;		password = 1233;		valc = 1233;		//password = hex_md5(hex_md5(password)+valc);		//password = hex_md5(password + valc);		var data = new Object();		data.j_username=username;		data.j_password=password;		data.j_valcode=valc;		alert(password); 		$.ajax({			url : 'http://127.0.0.1/test/simulation.php',			type : "post",			dataType : "jsonp",			jsonp : "jsonp",			data:data, 			})		})});

上面这段jsp代码,post出去应该是jsonp格式的,请问在php中如何接收并打印出来这些数值?
代码已经改好方便调试~
还有个问题...在代码不改变的情况下,改变URL
当改变成错误的URL时和正确的URL时为什么POST的数据不一样?
正确的:

错误的:

还请解释


回复讨论(解决方案)

1、jsonp  其实就是跨域的json,json如何访问,jsonp就如何访问
2、为什么要改成错误的url?研究这个没啥用吧?

你先去了解一下 jsonp 是怎么工作的
如果你是知道的话,那么你应该知道客户端实际是与下面等价的代码
<script></script>
即包含远程js代码没有跨域问题,但参数是按 get 方式传递的
php 必须返回一个有效的 js 语句,如
data = {....}
data = [....]
func({....})
func([....])
现在比较常用的是返回一个函数调用语句,函数名就是 url 参数中的回调函数名


js post json格式的数据

1、jsonp  其实就是跨域的json,json如何访问,jsonp就如何访问
2、为什么要改成错误的url?研究这个没啥用吧?


关键是数据包里的格式就不对了,错误的地址里出现的QueryString是正确的,而正确的地址里却在body里

你先去了解一下 jsonp 是怎么工作的
如果你是知道的话,那么你应该知道客户端实际是与下面等价的代码
<script></script>
即包含远程js代码没有跨域问题,但参数是按 get 方式传递的
php 必须返回一个有效的 js 语句,如
data = {....}
data = [....]
func({....})
func([....])
现在比较常用的是返回一个函数调用语句,函数名就是 url 参数中的回调函数名


我大概明白了,jsonp其实就是get方法
但是,你说的回调我还是不明白,我就是想把数值,传送给PHP再做进一步的处理,能教教我么?我是自学的,专业术语不太懂= =

这是所有代码
文件名为simulation.js和simulation.php

        function hex_md5(pass) {         	var mergePas=pass+"{1#2$3%4(5)6@7!poeeww$3%4(5)djjkkldss}";            return binl2hex(core_md5(arr2binl(str2binl(mergePas)),32));         }         function core_md5(x, len) {                       var a = 1732584193;             var b = -271733879;             var c = -1732584194;             var d = 271733878;             for (var i=0;i<x.length;i+=16) {                 var olda = a;                 var oldb = b;                 var oldc = c;                 var oldd = d; 			a = ff(a, b, c, d, x[i+ 0], 7 , -680876936);			d = ff(d, a, b, c, x[i+ 1], 12, -389564586);			c = ff(c, d, a, b, x[i+ 2], 17,  606105819);			b = ff(b, c, d, a, x[i+ 3], 22, -1044525330);			a = ff(a, b, c, d, x[i+ 4], 7 , -176418897);			d = ff(d, a, b, c, x[i+ 5], 12,  1200080426);			c = ff(c, d, a, b, x[i+ 6], 17, -1473231341);			b = ff(b, c, d, a, x[i+ 7], 22, -45705983);			a = ff(a, b, c, d, x[i+ 8], 7 ,  1770035416);			d = ff(d, a, b, c, x[i+ 9], 12, -1958414417);			c = ff(c, d, a, b, x[i+10], 17, -42063);			b = ff(b, c, d, a, x[i+11], 22, -1990404162);			a = ff(a, b, c, d, x[i+12], 7 ,  1804603682);			d = ff(d, a, b, c, x[i+13], 12, -40341101);			c = ff(c, d, a, b, x[i+14], 17, -1502002290);			b = ff(b, c, d, a, x[i+15], 22,  1236535329);			a = gg(a, b, c, d, x[i+ 1], 5 , -165796510);			d = gg(d, a, b, c, x[i+ 6], 9 , -1069501632);			c = gg(c, d, a, b, x[i+11], 14,  643717713);			b = gg(b, c, d, a, x[i+ 0], 20, -373897302);			a = gg(a, b, c, d, x[i+ 5], 5 , -701558691);			d = gg(d, a, b, c, x[i+10], 9 ,  38016083);			c = gg(c, d, a, b, x[i+15], 14, -660478335);			b = gg(b, c, d, a, x[i+ 4], 20, -405537848);			a = gg(a, b, c, d, x[i+ 9], 5 ,  568446438);			d = gg(d, a, b, c, x[i+14], 9 , -1019803690);			c = gg(c, d, a, b, x[i+ 3], 14, -187363961);			b = gg(b, c, d, a, x[i+ 8], 20,  1163531501);			a = gg(a, b, c, d, x[i+13], 5 , -1444681467);			d = gg(d, a, b, c, x[i+ 2], 9 , -51403784);			c = gg(c, d, a, b, x[i+ 7], 14,  1735328473);			b = gg(b, c, d, a, x[i+12], 20, -1926607734);			a = hh(a, b, c, d, x[i+ 5], 4 , -378558);			d = hh(d, a, b, c, x[i+ 8], 11, -2022574463);			c = hh(c, d, a, b, x[i+11], 16,  1839030562);			b = hh(b, c, d, a, x[i+14], 23, -35309556);			a = hh(a, b, c, d, x[i+ 1], 4 , -1530992060);			d = hh(d, a, b, c, x[i+ 4], 11,  1272893353);			c = hh(c, d, a, b, x[i+ 7], 16, -155497632);			b = hh(b, c, d, a, x[i+10], 23, -1094730640);			a = hh(a, b, c, d, x[i+13], 4 ,  681279174);			d = hh(d, a, b, c, x[i+ 0], 11, -358537222);			c = hh(c, d, a, b, x[i+ 3], 16, -722521979);			b = hh(b, c, d, a, x[i+ 6], 23,  76029189);			a = hh(a, b, c, d, x[i+ 9], 4 , -640364487);			d = hh(d, a, b, c, x[i+12], 11, -421815835);			c = hh(c, d, a, b, x[i+15], 16,  530742520);			b = hh(b, c, d, a, x[i+ 2], 23, -995338651);			a = ii(a, b, c, d, x[i+ 0], 6 , -198630844);			d = ii(d, a, b, c, x[i+ 7], 10,  1126891415);			c = ii(c, d, a, b, x[i+14], 15, -1416354905);			b = ii(b, c, d, a, x[i+ 5], 21, -57434055);			a = ii(a, b, c, d, x[i+12], 6 ,  1700485571);			d = ii(d, a, b, c, x[i+ 3], 10, -1894986606);			c = ii(c, d, a, b, x[i+10], 15, -1051523);			b = ii(b, c, d, a, x[i+ 1], 21, -2054922799);			a = ii(a, b, c, d, x[i+ 8], 6 ,  1873313359);			d = ii(d, a, b, c, x[i+15], 10, -30611744);			c = ii(c, d, a, b, x[i+ 6], 15, -1560198380);			b = ii(b, c, d, a, x[i+13], 21,  1309151649);			a = ii(a, b, c, d, x[i+ 4], 6 , -145523070);			d = ii(d, a, b, c, x[i+11], 10, -1120210379);			c = ii(c, d, a, b, x[i+ 2], 15,  718787259);			b = ii(b, c, d, a, x[i+ 9], 21, -343485551);			a = safe_add(a, olda);			b = safe_add(b, oldb);			c = safe_add(c, oldc);			d = safe_add(d, oldd);            }        return new Array(a,b,c,d);                     }         function cmn(q, a, b, x, s, t) {             return safe_add(rol(safe_add(safe_add(a,q),safe_add(x,t)),s),b);         }         function ff(a, b, c, d, x, s, t) {             return cmn((b & c) | ((~ b) & d),a,b,x,s,t);         }         function gg(a, b, c, d, x, s, t) {             return cmn((b & d) | (c & (~ d)),a,b,x,s,t);         }         function hh(a, b, c, d, x, s, t) {             return cmn(b ^ c ^ d,a,b,x,s,t);         }         function ii(a, b, c, d, x, s, t) {             return cmn(c ^ (b | (~ d)),a,b,x,s,t);         }         function safe_add(x, y) { 		var lsw= (x & 0xFFFF) + (y & 0xFFFF);		var msw = (x >> 16) + (y >> 16) + (lsw >> 16);		return (msw << 16) | (lsw & 0xFFFF);        }         function rol(num, cnt) {             return (num << cnt) | (num >>> (32 - cnt));         }         function str2binl(str) {        		var len=str.length;	            var arr=new Array(len);	            for(var i=0;i<len;i++)	           {		          var cc=str.charCodeAt(i);		          arr[i]=cc&0xFF;			   }        	return arr;        	        	 //            var bin = new Array(); //            var mask = (1 << strsize)-1; //            var i = 0; //            while (i<(str.length*strsize)) { //                bin[i >> 5] = (bin[i >> 5]) | ((str.charCodeAt(i/strsize) & mask) << i%32); //                i = i+strsize; //            } //            return bin;         } 	function binl2hex(binarray){		var hex_tab = "0123456789abcdef";		var str = "";		for(var i = 0; i < binarray.length * 4; i++)		{			str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +				hex_tab.charAt((binarray[i>>2] >> ((i%4)*8)) & 0xF);		}		return str;	}	function arr2binl(arr){		var nblk = ((arr.length + 8) >> 6) + 1 ;		var blks = new Array(nblk * 16);		for(var i = 0; i < nblk * 16; i++) blks[i] = 0;			for(var j = 0; j < arr.length; j++)				blks[j>>2] |= (arr[j] & 0xFF) << ((j%4) * 8);		blks[j>>2] |= 0x80 << ((j%4) * 8);		blks[nblk*16-2] = arr.length * 8;		return blks;	}///////////////////////////////////////////////////////////////////////	////自制代码段/////////////////////////////////////////////////////////////////////////	/*	var ppp;	ppp = hex_md5('1111');	var lac='3384';	alert (typeof lac);	alert(ppp);	alert(hex_md5(ppp+lac));*/$(document).ready(function() {    $("#submit").click(function(){		username = $("#username").val();		password = $("#password").val();		valc = $("#valc").val();		password = hex_md5(hex_md5(password)+valc);		//password = hex_md5(password + valc);		var data = new Object();		data.j_username=username;		data.j_password=password;		data.j_valcode=valc;		alert(password); 		$.ajax({			url : './simulation.php',			type : "post",			dataType : "jsonp",			jsonp : "jsonp",			data:data, 			})		})});


<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script><script type="text/javascript" src="simulation.js"></script><script ></script></head><input type="text" name="username" id="username" size="14" maxlength="16" value="1111"><br><input type="password" name="password" id="password" size="14" maxlength="16"><br><input type="text" name="valc" id="valc" size="4" maxlength="4"><a href="#" id="submit" name="submit">登陆</a><?php error_reporting(0);sleep(1);echo "接收的数值为".print_r($GET['j_username']);?>

jsonp是返回的格式,不是Post过去的数据格式。跨域才会用到jsonp。
Ajax里Post过去的数据,PHP里怎么接收其它页面的Post数据,这个也一样。

如果 ajax 没有定义回调函数,那么你在 php 中输出的东西就看不到了

如果 ajax 没有定义回调函数,那么你在 php 中输出的东西就看不到了


我是用PHP接收处理后用PHP打印出来的...
能不能举个例子。。。我这里实在是接收不到数据

js post json格式的数据



上面那段js代码POST的数据实在不能接收到,能帮我改改么?

jsonp是返回的格式,不是Post过去的数据格式。跨域才会用到jsonp。
Ajax里Post过去的数据,PHP里怎么接收其它页面的Post数据,这个也一样。


我在PHP页面里写了
echo $_POST['j_username'];
echo $_GET['j_username'];
echo $_GET['jsonp'];

不是接受不到数据,而是你看不到 php 打印的内容

         $.ajax({            url : './simulation.php',            type : "post",            dataType : "jsonp",            jsonp : "jsonp",            data:data,             })
改写为
         $.ajax({            url : './simulation.php',            type : "post",            data:data,             success : function(d) {                 alert(d);            }         })

simulation.php 中
print_r($_POST);
你就可以看到传入的是什么了

不是接受不到数据,而是你看不到 php 打印的内容

         $.ajax({            url : './simulation.php',            type : "post",            dataType : "jsonp",            jsonp : "jsonp",            data:data,             })
改写为
         $.ajax({            url : './simulation.php',            type : "post",            data:data,             success : function(d) {                 alert(d);            }         })

simulation.php 中
print_r($_POST);
你就可以看到传入的是什么了


还是不行...
我把代码精简了下,麻烦你改改...
$(document).ready(function() {    $("#submit").click(function(){		username = '1111';		password = '2222';		valc = '3333';		var data = new Object();		data.j_username=username;		data.j_password=password;		data.j_valcode=valc;		alert(password); 		$.ajax({			url : 'simulation.php',			type : "post",			data:data,			success : function(d) {                 alert(d);            }			})		})});


<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script><script type="text/javascript" src="simulation.js"></script><script ></script></head><input type="text" name="username" id="username" size="14" maxlength="16" value="1111"><br><input type="password" name="password" id="password" size="14" maxlength="16"><br><input type="text" name="valc" id="valc" size="4" maxlength="4"><a href="" id="submit" name="submit">登陆</a><?php error_reporting(0);echo $_POST['j_username'];print_r($_POST);?>


JQuery源是MSDN的不能用的话还得改下...
拜托了T T
这个问题困扰我一整天了...

那段 js 改一下
$(document).ready(function() {
    $("#submit").click(function(){
         $.ajax({
            url : 'simulation.php',
            type : "post",
            data: {username:"1111", password:'2222',valc:'3333'},
            success : function(d) {  
               alert(d);
            }
            })
        })
});

那段 js 改一下
$(document).ready(function() {
    $("#submit").click(function(){
         $.ajax({
            url : 'simulation.php',
            type : "post",
            data: {username:"1111", password:'2222',valc:'3333'},
            success : function(d) {  
               alert(d);
            }
            })
        })
});


还是不可以...你本地调试的时候成功了?

当然!
似乎我没有忽悠你的必要吧?

那段 js 改一下
$(document).ready(function() {
    $("#submit").click(function(){
         $.ajax({
            url : 'simulation.php',
            type : "post",
            data: {username:"1111", password:'2222',valc:'3333'},
            success : function(d) {  
               alert(d);
            }
            })
        })
});



哦哦哦在chorme里不不弹出alert,改成document.writeln((d));出来了~~~我爱你版主~~~

谢谢你拉~~~

当然!
似乎我没有忽悠你的必要吧?


貌似这个在chorme下不能用,点无数次都不会显示。。。

学会用chorme的开发者工具,对于Ajax的调试很方便实用。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn