且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery的AJAX调用一个ASP.NET的WebMethod

更新时间:2023-01-06 22:00:33

您真的应该确保正确连接$ C C此JSON $。 JSON.stringify 是最可靠的方法:

You should really make sure to properly encode this JSON. JSON.stringify is the most reliable method:

data: JSON.stringify({ accessCode: code, newURL: url })

这保证了即使 code 网​​址变量包含一些危险人物会破坏你的在生成的JSON在结束一切字符串连接将是正确的连接codeD。该JSON.stringify方法natievly建成现代的浏览器,但如果你需要支持旧的,你可以包括 json2.js

This ensures that even if the code and url variables contain some dangerous characters that will break your string concatenations in the resulting JSON at the end everything will be properly encoded. The JSON.stringify method is natievly built into modern browsers but if you need to support legacy you could include json2.js.

此外,因为你code是不再阻塞,你应该确保,如果你把这个 sendUpdate 一些按钮点击或表单提交取消默认的行动返回false。

Also because you code is no longer blocking you should make sure that if you call this sendUpdate from some button click or form submit you cancel the default action by returning false.