开发者

JSON decode problems

开发者 https://www.devze.com 2023-01-24 14:10 出处:网络
When I post a text with Ajax, like this: var t开发者_开发技巧ext = encodeURIComponent(transtext);

When I post a text with Ajax, like this:

var t开发者_开发技巧ext = encodeURIComponent(transtext);

and receive it with PHP:

$text=$_POST['text']

it seems that PHP automatically decodes the encoded string. Is there any way to avoid this?

Ok here is my original Json string that i want to post with ajax:

{"1":"\\"sdfsdfsdf" as&das&d ","2":"asdasd"}

I have to encodeURIComponent the string inside so i can post it json.strigify that dont encode &:

{"1":"%22asdasd%5C%22asdasd%22asdasd%26asdasd","2":"asdasd"}

But when i receive the string in PHP the " make fail the jsondecode because i get

POST={"1":""sdfsdfsdf" as&das&d ","2":"asdasd"}

Any ideas?


This function (encodeURIComponent()) does not produce JSON.

It produces an URL-encoded string, and these are decoded by the server automatically. I don't see why you would not want that.

URL-encoding is a pure transport encoding, its purpose in life is to make sure the data you send to a server is suitable for transfer via HTTP GET or POST, and that it is not garbled on its way. That the server decodes it for you before filling the $_GET, $_POST and $_REQUEST arrays is the right thing to do. It makes sure the data is in the same shape it was before it was sent from the client.

0

精彩评论

暂无评论...
验证码 换一张
取 消