Home >Web Front-end >JS Tutorial >Instructions for using ajax to call json data in jquery_jquery
The two methods $.get() and $.post() are basically used the same way, so I only talk about $.post()!
Scenario 1: $.post("url",function); The data returned at this time has not been processed, so it is not in json format!
Scenario 2: $.post("url",function, "json"); Although the returned data is specified to be in json format, it is actually not!
Case 3: $.post("url",{},function); The parameter passed in at this time is empty, and the returned data format is not specified, so it is not in json format!
Scenario 4: $.post("url",{},function, "json"); correctly returns data in json format!
Key points: When you want to process the returned data as json format, you must pass in parameters (if the parameters are empty, write {}), and you must also specify the return type as "json"!
$.ajax({
url:"url",
dataType:"json",
type:"get"
success:function
})
Key points: To specify dataType as "json", you will get json format data regardless of the get or post method. However, I suggest that in order to be consistent with the above two methods, it is best to add the condition data: {}.
$.getJSON("url",function)
Key point: Obtaining json format data through get is a convenient way of writing $.get()!