Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-03 07:46:33 09月18日 Update

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.

Tags: Code procedure content character analysis processing character type special unexpected success boss professional small and medium rich in content parameters fields strings objects that is quotation marks Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Huawei Linux vpn Microsoft