最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - what's the proper way to sanitize data when I'm using jquery to send post data? - Stack Overflow

programmeradmin1浏览0评论

If the post data contains "&" character it thinks it's separating query string parameters. If it contains + it thinks it's a space. I'm sure there's some prebuilt function that takes care of these things already.

If the post data contains "&" character it thinks it's separating query string parameters. If it contains + it thinks it's a space. I'm sure there's some prebuilt function that takes care of these things already.

Share Improve this question asked Dec 11, 2010 at 11:20 ForeverConfusedForeverConfused 1,7773 gold badges27 silver badges46 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Just pass your data as an object and jQuery will serialize it via $.param() internally, for example:

$.ajax({
  //options..
  data: { key: "myValue" }
});
//the same goes for shorthand methods:
$.post("url", { key: "myValue" });

All the magic is basic JavaScript though, $.param() just uses encodeURIComponent() underneath to do the serialization (including & encoding) when creating the string.

If you're sending an entire <form> just use .serialize() which serializes the entire <form> (all successful form elements) to the string - like a normal non-AJAX submit would, for example:

$.post("url", $("form").serialize());

Another option is encodeURI

http://www.w3schools./jsref/jsref_encodeURI.asp

发布评论

评论列表(0)

  1. 暂无评论