js's ajax in one step

*, ajax call syntax

$.ajax({//ajax gets data asynchronously
	type:'POST',//Request method
	dataType:'json',//Returns the data format, if the format is not specified as json, the data data obtained below is only a normal string
	url: '<%=request.getContextPath()%>/yourAction.do?method=yourmethod&curLevel='+curLevel,
	success: function(data){//The result returned according to the set data format
	    var option = "";
	    $("#upId").html("");//Clear the original data
		$.each(data, function (n, st) {//parse data, html splicing
	       option += "<option value='"+st.id+"'>"+st.specialName + "</option>";
	   });
	   $("#upId").append(option);
	}
});

 

*, ajaxSubmit calling syntax

jQuery(document.getElementsByName('yourForm')).ajaxSubmit( {//Get a form
    dataType:'json',//The type of the returned information, if the format is not specified as json, the data data obtained below is only a normal string
    success : function(data) {
	if (data.success) {//It can be used directly as a json object
	    alert(data.msg);
		
	} else {
	    alert(data.msg);
	}
    }
});

 

*, what to do in the background?

The background processing borrows classes such as net.sf.json.JSON* to convert the entity object into a string of json object and return it to the foreground in the form of a stream;
Such as:
response.setHeader("Content-type", "text/html");
response.setCharacterEncoding(charset);
response.getWriter().flush();
response.getWriter().print(JSON*class.fromObject(entity object).toString());

 

-------------------------------------------------- --------Small accident---------------------------------------- ------------------------

*, When there is no response after ajax click, carefully check the content returned by the error function

error: function (xhr) {alert(xhr.responseText) }

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326710161&siteId=291194637