Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the ajax parameter of jQuery skillfully to use JSON?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This issue of the content of the editor will bring you about jQuery ajax reference skillful use of JSON how to use the process, the article is rich in content and professional analysis and description for you, after reading this article, I hope you can get something.

The ajax call of jQuery is very convenient, and you like to use the data format of Json when passing parameters. For example:

The code is as follows:

Function AddComment (content) {

Var threadId = $("# span_thread_id") .html ()

Var groupId = $("# span_group_id") .html ()

Var groupType = $("# span_group_type") .html ()

Var title = $("# thread_title") .html ()

Var content = content.replace (/\ x22andggamo'"')

$.ajax ({

Url:'/ WebService/GroupService.asmx/AddThreadComment'

Data:'{threadId:' + threadId +', groupId:' + groupId +', groupType:' + groupType +', title: "'+ title +'", content: "'+ content +'"}', type: 'post'

DataType: 'json'

ContentType: 'application/json;charset=utf-8'

Cache: false

Success: function (data) {

/ / judge whether it is successful according to the returned value data.d

}

Error: function (xhr) {

/ / if an exception occurs in the middle, check xhr.responseText

}

});

}

The most troublesome and error-prone thing is to concatenate Json strings, add quotation marks to the values of character parameters, and give special treatment to', /, etc. for the text fields entered by the user.

As an unexpected opportunity, my boss recommended me a new method. Look at the following code:

The code is as follows:

Function AddComment (content) {

Var comment = {}

Comment.threadId = $("# span_thread_id") .html ()

Comment.groupId = $("# span_group_id") .html ()

Comment.groupType = $("# span_group_type") .html ()

Comment.title = $("# thread_title") .html ()

Comment.content = content

$.ajax ({

Url:'/ WebService/GroupService.asmx/AddThreadComment'

Data: $.toJSON (comment)

Type: 'post'

DataType: 'json'

ContentType: 'application/json;charset=utf-8'

Cache: false

Success: function (data) {

/ / processed according to the returned value data.d

}

Error: function (xhr) {

/ / an exception occurred in the middle. Check xhr.responseText for more information.

}

});

}

Just use $.toJSON (object) directly.

JSON plug-in for jQuery: http://code.google.com/p/jquery-json/

The above is the process of using JSON skillfully in the ajax of jQuery shared by Xiaobian. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report