How do I correctly return JSON data to an AJAX request in the Crystal Portlet?

Source: Internet
Author: User

How can the JSON data be returned correctly and conveniently when an AJAX request is required in the crystal portlet and the backend is returned with JSON data?

Here are two ways to:

Method One: Ajax requests, using Renderurl, corresponding to the Portlet class using the Ajax (data) method to return Java objects;

Method Two: Ajax requests, using ResourceUrl, corresponding to the Portlet class using the Ajax (Data,response) method to output the Java object directly into the response stream; (Recommended this method)

Step into the Guide

Method One:

  1. When Ajax requests, URLs are generated in Renderurl, with no differences from ordinary portlet requests;
  2. The corresponding method is created in the Portlet class that corresponds to the access, and is returned in Ajax (data), as shown in the following code example:

    @RequestMapping (params"action=getuserbyid") Public String Ajaxrenderurltest (@RequestParam string id) {    = Userservice.getuserbyid (ID);     return Ajax (user);}
  3. At this point, the "common/_ajax.jsp file could not be found" error may appear, as follows:
    1. Make sure the project uses the latest version Crystal-portlet-core 2.4 snapshot and above version;
    2. In the Portlet's/web-inf/spring/common/jstl-portlet.xml file, replace the configuration of the Viewresolver bean with the following configuration:

      <bean id="Viewresolver"    class="Com.gsoft.crystal.portlet.CrystalInternalResourceViewResolver" Abstract="true"> <property name="Viewclass"value="Org.springframework.web.servlet.view.JstlView"/> <property name="prefix"Value="/web-inf/jsp/"/> <property name="Baseprefix"Value="/web-inf/jsp"/> <property name="suffix"Value=". JSP"/> <property name="ContentType"Value="Text/html;charset=utf-8"/> <property name="Order"Value="1"/></bean>
        1. Class is replaced with Com.gsoft.crystal.portlet.CrystalInternalResourceViewResolver;

        2. Add the Baseprefix variable, the value of which is the root directory of the JSP file, such as:/web-inf/jsp.

Method Two: (Recommended use)

  1. When Ajax requests, URLs are generated using ResourceUrl;
  2. The corresponding method is created in the Portlet class that corresponds to the access, and is returned in Ajax (Data,response), as shown in the following code example:

    @ResourceMapping ("testajax")publicvoid  ajaxtest ( Resourceresponse response, @RequestParam String ID) throws IOException {    = Userservice.getuserbyid (ID); C11/>ajax (user, response);}
  3. The foreground AJAX call sample code is as follows:

    $.ajax ({    type:'post',    URL:'<portlet: ResourceUrl id= "Testajax"/>',    data:{        <portlet:namespace/ >id:    success:function (data) {            }});

How do I correctly return JSON data to an AJAX request in the Crystal Portlet?

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.