jsp USES jquery+ajax to pass json format parameters between front and back

  • 2020-06-12 10:17:01
  • OfStack

After a period of experimental consideration, the parameters passing between the front and background were finally completed. The experimental tool myeclipse+structs1.2 was used.

data: {parameter: ""} and data:" parameter = "+ variable.

2. The data transferred from the background to the foreground shall be converted into json format, and the steps shall be well mastered.

3. The use of jquery in js must be referenced, or the jquery statement will not be executed. It took me half a day to solve this problem. jquery reference process is as follows: download jQuery. js, ES19en-1.4.2.min. js two js files from the Internet and put them in the folder js under webroot, the reference code is as follows:

< script src=" < %=path% > /js/jQuery.js" language="javascript"type="text/javascript" > < /script >

< script src=" < %=path% > /js/jquery-1.4.2.min.js" language="javascript"type="text/javascript" > < /script >

Among them < %=path% > Represents the root directory wenroot file directory.

4. url path must be written correctly.

5. When the current stage does not pass parameters to the background, data can be used without writing or data:{} instead.

The foreground code is as follows:
 
<span style="white-space:pre"> </span>var checkValue=$("#s1").val(); 

 
<span style="white-space:pre"> </span>// this var Is to obtain the id q s1 the select Select the opention value  
$ 
.ajax({ 
type : "post", 
url : "getShowDataList.do", 
async : true, 
//data:{data:""}, This one works as well  
data : 
"filepath="+checkValue 

 
<span style="white-space:pre"> </span>//data: Is the data passed to the background, where the data format is json format  
, 
dataType : "json", 
error : function() { 
//alert(checkValue); 
alert(' Failed to load! '); 
}, 
success : function(json) { 

 
<span style="white-space:pre"> </span>// Here, json It's the data that's being passed in the background, and in this case, the data format json format  

The foreground gets the list data set in json format in the background, which is written in function
 
var points = [];// Create an array  
for ( var i = 0; i < json.length; i++) { 

var str = new OpenLayers.LonLat(json[i].lon, 
json[i].lat); 
points.push(str); 

} 

Background code:
 
public ActionForward execute(ActionMapping mapping, ActionForm form, 
HttpServletRequest request, HttpServletResponse response) { 

String filepath = request.getParameter("filepath"); 

 
<span style="white-space:pre"> </span>// Get the ones passed by the front desk filepath 
System.out.println(filepath); 

List<Show> datalist = getShowData(filepath); 

response.setContentType("appliction/json;charset=utf-8"); 
JSONArray jsonArray = JSONArray.fromObject(datalist); 

 
try { 

PrintWriter out = response.getWriter(); 
out.print(jsonArray); 
for (int i = 0; i < jsonArray.size(); i++) { 
System.out.println(jsonArray.get(i)); 
} 
out.flush(); 
} catch (Exception e) { 
e.printStackTrace(); 
} 
return null; 
} 

Related articles: