Ajax post方法,使用application / json作为contentType时返回400错误,使用application / x-www-form-urlencoded时返回500错误

我想通过jQuery进行Ajax调用,我以这种格式将数据发布为对象。

var data = {
pointsPriceChangeInfo: {oldTripTotal: "123",oldTripGrandTotal: "1231"}
prices: "[{"price":{"amount":"$2123","currencyCode":"USD"},"type":"PER_TRAVELER"}]"
productInternalName: "Travel Protection"
responseId: "test"
tripId: "***-99ff-417e-***-****"
}

$.ajax({
  url: URL,method: "POST",data: JSON.stringify(data),contentType: "application/json"
})

这给了我400错误:

Request URL: https://***/Checkout/addGlobalInsuranceV2
Request Method: POST
Status Code: 400 
Remote Address: *********
Referrer Policy: no-referrer-when-downgrade

accept: */*
accept-Encoding: gzip,deflate,br
accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 1649
Content-Type: application/json; charset=UTF-8

当我将ajax调用更改为此时:

 $.ajax({
     url: URL,data: data
 })

请求给了我500错误:

Request URL: https://***/Checkout/addGlobalInsuranceV2
Request Method: POST
Status Code: 500 
Remote Address: ***.*.*.*
Referrer Policy: no-referrer-when-downgrade

accept: */*
accept-Encoding: gzip,zh;q=0.7
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 1687
Content-Type: application/x-www-form-urlencoded; charset=UTF-8

这是Java方法的标志:

public Map<String,Object> addGlobalInsuranceV2(
            @RequestParam(PRODUCT_INTERNAL_NAME_KEY) String productInternalName,@RequestParam(RESPONSE_ID_KEY) String responseId,@RequestParam(TRIP_ID_KEY) String tripId,@RequestParam(PRICES_KEY) String prices,HttpServletRequest httpRequest,@ModelAttribute(value = POINTS_INFO) PointsPriceChangeInfo pointsPriceChangeInfo)

有人可以告诉我为什么将application / json设置为400时为什么是400,为什么要设置为500?

谢谢

yjr1119 回答:Ajax post方法,使用application / json作为contentType时返回400错误,使用application / x-www-form-urlencoded时返回500错误

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3134730.html

大家都在问