How does PHP receive JavaScript post data?

Source: Internet
Author: User
Tags php print what php
$ (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 ();d ata.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,})}) ;

Above this JSP code, post out should be JSONP format, how to receive and print these values in PHP?
Code has been changed to facilitate debugging ~
There's one more question ... Change the URL in case the code does not change
Why is the post data different when it is changed to the wrong URL and the correct URL?
Correct:

The wrong:

Please also explain


Reply to discussion (solution)

1, Jsonp is actually cross-domain Json,json How to access, JSONP on how to access
2, why to change to the wrong URL? It's no use studying this, is it?

You go first and see how JSONP works.
If you are aware, then you should know that the client is actually a code equivalent to the following

That is, there is no cross-domain problem with the remote JS code, but the parameters are passed by Get mode
PHP must return a valid JS statement, such as
Data = {...}
data = [....]
Func ({...})
Func ([...])
It is now more common to return a function call statement, which is the name of the callback function in the URL parameter


JS Post JSON-formatted data

1, Jsonp is actually cross-domain Json,json How to access, JSONP on how to access
2, why to change to the wrong URL? It's no use studying this, is it?


The key is that the format of the packet is not correct, the wrong address in the QueryString is right, and the correct address in the body

You go first and see how JSONP works.
If you are aware, then you should know that the client is actually a code equivalent to the following

That is, there is no cross-domain problem with the remote JS code, but the parameters are passed by Get mode
PHP must return a valid JS statement, such as
Data = {...}
data = [....]
Func ({...})
Func ([...])
It is now more common to return a function call statement, which is the name of the callback function in the URL parameter


I probably understand, Jsonp is actually a get method.
But, you say the callback I still do not understand, I just want to send the value, to PHP to do further processing, can teach me? I am self-taught, professional terminology is not very understanding =

This is all code
File name is simulation.js and 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
 
  
>) + (y >> +) + (LSW >> +); 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
  
   
> 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 *); 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 &GT;&GT;2] |= 0x80 << ((j%4) * 8); Blks[nblk*16-2] = arr.length * 8;return blks;} Self-made code snippet/////////////////////////////// *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 ();p Assword = $ ("#password"). Val (); Valc = $ ("#valc"). Val ();p assword = Hex_md5 (HEX_MD5 ( Password) +valc)//password = hex_md5 (password + valc); var data = new Object ();d Ata.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,})});
  
 


 
  

Landing

JSONP is the format returned, not the data format of post past. The JSONP is used across domains.
Ajax post data in the past, PHP how to receive other pages of post data, this is the same.

If Ajax does not have a callback function defined, then what you output in PHP will not be visible.

If Ajax does not have a callback function defined, then what you output in PHP will not be visible.


I use PHP to receive processing after the use of PHP print out ...
Can you give me an example ... I really can't get the data here.

JS Post JSON-formatted data



The above section of the JS code post data can not be received, can help me change it?

JSONP is the format returned, not the data format of post past. The JSONP is used across domains.
Ajax post data in the past, PHP how to receive other pages of post data, this is the same.


I wrote it on the PHP page.
echo $_post[' J_username '];
echo $_get[' J_username '];
echo $_get[' Jsonp '];

Instead of receiving data, you can't see what PHP is printing

         $.ajax ({            URL: './simulation.php ',            type: "Post",            DataType: "Jsonp",            jsonp: "Jsonp",            data:data,< c6/>})
Rewritten as
         $.ajax ({            URL: './simulation.php ',            type: "Post",            Data:data,             success:function (d) {                 alert (d);            }         })

In simulation.php
Print_r ($_post);
You can see what's coming in.

Instead of receiving data, you can't see what PHP is printing

         $.ajax ({            URL: './simulation.php ',            type: "Post",            DataType: "Jsonp",            jsonp: "Jsonp",            data: Data,             })
Rewritten as
         $.ajax ({            URL: './simulation.php ',            type: "Post",            Data:data,             success:function (d) {                 alert (d);            }         })

In simulation.php
Print_r ($_post);
You can see what's coming in.


Or not ...
I have reduced the code, I would trouble you to change ...
$ (document). Ready (function () {    $ ("#submit"). Click (function () {username = ' 1111 ';p assword = ' 2222 '; valc = ' 3333 '; var data = new Object ();d ata.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);            }})});


 
  

Landing


The jquery source is not available on MSDN and has to be changed ...
Please, T-T.
This problem has been bothering me all day ...

That JS change
$ (document). Ready (function () {
$ ("#submit"). Click (function () {
$.ajax ({
URL: ' simulation.php ',
Type: "Post",
Data: {username: "1111", Password: ' 2222 ', Valc: ' 3333 '},
Success:function (d) {
alert (d);
}
})
})
});

That JS change
$ (document). Ready (function () {
$ ("#submit"). Click (function () {
$.ajax ({
URL: ' simulation.php ',
Type: "Post",
Data: {username: "1111", Password: ' 2222 ', Valc: ' 3333 '},
Success:function (d) {
alert (d);
}
})
})
});


Or not to ... Did you succeed in local debugging?

Of course!
Does it seem like I'm not kidding you?

That JS change
$ (document). Ready (function () {
$ ("#submit"). Click (function () {
$.ajax ({
URL: ' simulation.php ',
Type: "Post",
Data: {username: "1111", Password: ' 2222 ', Valc: ' 3333 '},
Success:function (d) {
alert (d);
}
})
})
});



Oh oh oh in the chorme don't pop alert, change to Document.writeln ((d)); Come out ~ ~ ~ I love you moderator ~ ~ ~

Thank you for pulling ~ ~ ~

Of course!
Does it seem like I'm not kidding you?


It seems that this can not be used under the chorme, point countless times will not show ...

Learn to use Chorme Developer tools, for Ajax debugging is very convenient and practical.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.