Ajax data transmission and reception format json

ajax There are three ways to submit and receive data are json $ .post (), $ get (), $ ajax () Default three kinds of operations are asynchronous, three operations are jQuery framework package.:

Asynchronous phenomenon: the client sends a request to the server, regardless of whether the server returns a response, the client can be with the   intention to do other things, you will not be stuck

How can there asynchronous operation synchronous operation

 

Synchronization phenomenon: the client sends a request to the server, the server returns a response as before, clients are in a wait   jammed state

First is the way the first post submitted

Format for the $ .post (

  url: // access server such as the url '$ {pageContext.request.contextpath} / ajaxWeb'

  data: // Write your json data such as '{ "name": "Joe Smith", "price": 1888}'

  callback: // callback function (data) {// data for the received data json }

  type: // type is the type of data received Why type such as json, text

);

Posted a look at the code specific examples

 

function postJson(){
		$.post(
			'${pageContext.request.contextPath}/ajaxWeb',
			'{"name":"张三","price":122}',
			function(data){
				alert(data);
			},
			'json'
		);
	}

 

  

Here's what the $ .get () mode

Let me talk about the way the format of $ .get

$.get(

  url: // url address to access the server

  data: // write data to json

  callback: // Writing callback function

  type: // why the received data format

);

Specific code:

$.get(
				'${pageContext.request.contextPath}/ajaxWeb',
				{"name":"z张三年","price":122},
				function(data){
					alert(data.name);
				},
				"json"
			)

  

The third is $ .ajax () this way

Let me talk about format

$.ajax(

  type: // designated as the post also get submitted

  url: // address submitted to the server

  contentType: // If you submit data to json format will write contentType: "application / json"

  success: function (data) {} // return data is executed successfully and

  error: function () {} // execution request function fails

  dataType: data types can be returned json can also be a text, if the front page newspaper undefined so should your meal will json data type instead of a flag, the children wrote datatype dataType

);

Example Code Sub paste:

$.ajax({
			type:'post',
			url:'${pageContext.request.contextPath}/ajaxWeb',
			data:{"name":"张三","price":123},
			success:function(data){
				alert(data.name);
			},
			error:function(){
				alert ( "request failed");
			},
			dataType: "json" // note that not datatype oh
		});

  

 

Guess you like

Origin www.cnblogs.com/tranquilityMan/p/11004844.html