Using the jquery Ajax post method or directly using the Jqery Ajax request action to get the returned JSON data __jsp

Source: Internet
Author: User

After mastering the interaction between Java objects and JSON, today's learning is how to use Ajax to request action and put back JSON characters, parsing JSON data in the foreground.

Step one: Use Ajax post to submit directly, write a post.jsp file

<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%> <!
DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

Then there is a JS (post.js) file corresponding to it, which is used to write the Post submission:

$ (function () {
	$ (' #bt '). Click (function () {
		var result=$ ("#name"). Val ();
		
		Use the $.post method to    submit
        $.post (  
            "postaction",//) specify the URL   
            {' result ': result},    	 //pass parameters, use Struts2 Injection parameters in the way (there is a corresponding property in action, setter method)
            function success (data) {//callback function
            	alert (data) after the successful commit;
                var r = $.parsejson (data);    Packet data resolves to JSON format     
          		alert (r.name);
                $ ("#result"). HTML (r.name);
            },   
            "JSON"   //Specifies that the returned data is a JSON string
        )
;

The corresponding postaction:

public class Postaction extends Actionsupport {
	private String result;
	Public String GetResult () {return result
		;
	}
	public void Setresult (String result) {
		This.result = result;
	}
	@Override public
	String execute () throws Exception {
		jsonobject jobject=new jsonobject ();		map<string,object> map=new hashmap<string,object> ();		map.put ("name", result);
		Jobject.element ("name", result);
		Result=jobject.tostring ();
		return Super.execute ();
	}
	

Struts configuration file:

? xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE struts Public "-//apache Software foundation//dtd struts Configuration 2.1//en" "http://struts.apache.org/dtds/ Struts-2.1.dtd ">
<struts>
	<package name=" Login "extends=" Json-default ">
			
		<action Name= "postaction" class= "edu.hue.jk.action.PostAction" >
			<result type=   "json" >
				<param Name = "Root" >result</param> <!--root indicates the hierarchy of the returned object             is the root result is the return results-->
			</result>
		</ action>
	</package>
</struts>    
results:

Click on the button:


Step two: Use Ajax directly to request action:ajax.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
    pageencoding=" UTF-8 "%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

Ajax.js:

$ (function () {
	$ (' #bt '). Click (function () {
		var name=$ ("#name"). Val ();
		$.ajax ({
  			type: Post),
              dataType: ' json ',//Accept data Format 
              cache:false,
              data:{' result ': name},
              URL: ' Ajaxaction ",
              beforesend:function (XMLHttpRequest) {
              },
              success:function (data) {
                alert (data);
                var r = $.parsejson (data);    Parses a string into a JSON object     
                	$ ("#result"). HTML (r.name);
                 
               },
              error:function () {
              //Request error Handling
                  alert ("Error !");
              }
           });
		
	});
	
});

Ajaxaction:

$ (function () {
	$ (' #bt '). Click (function () {
		var name=$ ("#name"). Val ();
		$.ajax ({
  			type: Post),
              dataType: ' json ',//Accept data Format 
              cache:false,
              data:{' result ': name},
              URL: ' Ajaxaction ",
              beforesend:function (XMLHttpRequest) {
              },
              success:function (data) {
                alert (data);
                var r = $.parsejson (data);    Parses a string into a JSON object     
                	$ ("#result"). HTML (r.name);
                 
               },
              error:function () {
              //Request error Handling
                  alert ("Error !");
              }
           });
		
	});
	
});

Struts configuration file:

	<package name= "Login" extends= "Json-default" > <action name= "ajaxaction" class= "
				Edu.hue.jk.action.AjaxAction ">
			<result   type=" json ">
				<param name=" root ">result</ Param> <!--root indicates that the hierarchy of the returned object is the root             result is the return results-->
			</result>
		</action>
	</ Package>

Results:




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.