AJAX请求ACTION的注意事项

本文详细介绍了在使用Struts2框架时如何正确配置JSON拦截器来处理以application/json格式提交的数据。强调了在使用$.ajax()时contentType的重要性,并提供了具体的配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先重点提示,contentType默认值为 “application/x-www-form-urlencoded”,不要设置为”application/json”,不要设置为”application/json”,不要设置为”application/json”,重要的事情说三遍!

否则使用 request.getParameter("userName")常规方法将无法获取数据,OH,MY GOD 。。。。。。。

$.ajax({

         //提交数据的类型 POST GET

         type:"POST",

         //提交的网址

         url:"${rootPath }web/dsm/ws/getEnergyUsageApi",

         //提交的数据

      data:JSON.stringify({'url':'http://119.23.119.27:7002/equipment/info','reqView.id':'_ALL_','httpMethod':'1'}),

         contentType:"application/json; charset=utf-8",

         //返回数据的格式

         datatype: "json",//"xml", "html", "script", "json", "jsonp", "text".

         //在请求之前调用的函数

         beforeSend:function(xhr){

              xhr.setRequestHeader("User-Agent", "headertest");

              xhr.setRequestHeader("Accept-Encoding", "testAE");

         },

         //成功返回之后调用的函数            

         success:function(data){

              console.log(data);

             //$("#msg").html(decodeURI(data));           

         }

      });

 

如果用STRUTS2接收数据,那么将十分痛苦,最好的办法是使用JSON拦截器,将JSON数据反序列化成入参

String contentType = request.getHeader("content-type");

...

if ((contentType != null) && contentType.equalsIgnoreCase("application/json")) {

    // load JSON object

    Object obj = JSONUtil.deserialize(request.getReader());

    ...

}

 

<!-- 指定在包名为actioncontroller结尾的包中扫描Action -->

       <constant name="struts.convention.package.locators" value="action,controller" />

      

<!--设置Convention映射的Action所在包的默认父包。默认值是convention-default-->

    <constant name="struts.convention.default.parent.package" value="json-space" />

      

       <package name="json-space" extends="json-default"> 

        <interceptors>

            <interceptor-stack name="myInterceptorStack">

                <interceptor-ref name="json"></interceptor-ref>

                <interceptor-ref name="defaultStack"></interceptor-ref>

            </interceptor-stack>

        </interceptors>        

        <default-interceptor-ref name="myInterceptorStack"></default-interceptor-ref>  

</package> 

注意上面加了<interceptor-ref name="json"></interceptor-ref>

 

上面是对所有ACTION进行拦截,也可以使用注解对单个ACTION拦截

<interceptor-stack name="interceptorStack1">

    <interceptor-ref name="json"/>

       <interceptor-ref name="defaultStack"/>

</interceptor-stack>

 

@Namespace("/")

@ParentPackage("json-space")

@InterceptorRef("interceptorStack1")

public class TestAction1 extends ActionSupport {

}

 

$.ajax()contentType为”application/json”时,提交的数据必须使用JSON.stringify()处理。如:

data:{'url':'http://119.23.119.27:7002/equipment/info','reqView.id':'_ALL_','httpMethod':'1'},

那么,STRUTS2将抛出异常:

org.apache.struts2.json.JSONException: Input string is not well formed JSON (invalid char u),另外传参必须用‘’号包围,'httpMethod':'1'不能写成httpMethod:'1'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值