ajax 前后端交互

1.前端

$.ajax({
    url: "http://localhost:8001/regist/regist",
    type: "post",
    /*date: JSON.stringify(registVO),
    contentType:"application/json",*/
    data:{
        "userName":$("#userName").val(),
        "password":$("#password").val()
    },
    dataType: "json",
    success: function (obj) {
        alert(obj);
    }
});

后端

@ApiOperation(value = "用户注册")
@RequestMapping(value = "/regist",method = RequestMethod.POST)
public JsonResult regist(RegistVO registVO) throws Exception{
    System.out.println("1232");
    System.out.println("123213123");

    logger.info("RegistController...regist...前端用户注册接口入参:[" + registVO.toString() + "]");

    return this.buildErrorResult();
}

 

2  前端

var registVO = {
    "userName":$("#userName").val(),
    "password":$("#password").val()
}
$.ajax({
    url: "http://localhost:8001/regist/regist",
    type: "post",
    date: JSON.stringify(registVO),
    contentType:"application/json",
    dataType: "json",
    success: function (obj) {
        alert(obj);
    }
});

后端

@ApiOperation(value = "用户注册")
@RequestMapping(value = "/regist",method = RequestMethod.POST)
public JsonResult regist(@RequestBody(required=false) RegistVO registVO) throws Exception{
    System.out.println("1232");
    System.out.println("123213123");

    logger.info("RegistController...regist...前端用户注册接口入参:[" + registVO.toString() + "]");

    return this.buildErrorResult();
}

结果: 第一种方法是可以的,但是第二种方法一直没有成功  contentType:"application/json", 因为这种传输模式会影响跨域请求,导致后端一直接受不到数据是null

个人的想法,若有不对请指出

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值