ajax mediatype,在ajax调用上获得“不支持的媒体类型-415” - Jquery,ajax(Getting an “Unsupported media type-415” on an ...

在ajax调用上获得“不支持的媒体类型-415” - Jquery,ajax(Getting an “Unsupported media type-415” on an ajax call- Jquery,ajax)

我正在调用ajax函数来更新某个记录,而我正在获得415-Unsupported Media类型。 以下是js:

$("#update").on("click", function(){

event.preventDefault();

return $.ajax('/update/record',{

method: 'PUT',

data:{

date: date

},

success: function(){

alert("record updated successfully");

}

});

HTML:

Update

我不知道我在这里做错了什么或者我需要添加什么来获得正确的ajax调用形式。任何想法????

谢谢!

I'm calling an ajax function to update a certain record and I'm getting a 415-Unsupported Media type. Below is the js:

$("#update").on("click", function(){

event.preventDefault();

return $.ajax('/update/record',{

method: 'PUT',

data:{

date: date

},

success: function(){

alert("record updated successfully");

}

});

html:

Update

I don't know what I'm doing wrong here or what I need to add to get the correct form of the ajax call.Any ideas????

Thanks!

原文:https://stackoverflow.com/questions/29707365

更新时间:2020-01-02 15:20

最满意答案

我相信你需要在ajax请求中设置contentType和dataType

contentType: "application/json",

dataType: "json",

I believe you need to set the contentType and dataType in your ajax request

contentType: "application/json",

dataType: "json",

相关问答

我之前发生过Spring @ResponseBody,这是因为没有接受发送请求头。 接受标题可能是一个痛苦的设置与jQuery,但这对我来说有用 $.postJSON = function(url, data, callback) {

return jQuery.ajax({

headers: {

'Accept': 'application/json',

'Content-Type': 'application/json'

},

...

看看几件事情: 这是它的样子: {

"type": "user",

"name": "teqni",

"regNumber": "12345",

"dob": "16-10-1992",

"email": "vihangshah16@gmail.com",

"password": "12345678",

"confirmPassword": "12345678",

"line1": "f-12",

"line2": "e ci

...

你试过设置吗? dataType: 'jsonp',

和"&callback=?" 在网址? 您正在另一个域上调用服务器,因此您必须将jsonp设置为dataType Have you tried setting dataType: 'jsonp',

and the "&callback=?" in the url? You are calling a server on another domain and so you must set jsonp as dataType

您需要在jQuery请求上设置内容类型。 如果你不这样做,它将默认为application/x-www-form-urlencoded 。 仅仅因为你添加@Consumes(MediaType.APPLICATION_FORM_URLENCODED)并不意味着JAX-RS不会将表单数据转换为Disruptive 。 需要有一个MessageBodyReader来处理那个转换,但是没有。 MediaType.TEXT_HTML 。 如果没有转换器来处理转换,只需添加即可。 删除这两个。 你想要的是处

...

我相信你需要在ajax请求中设置contentType和dataType contentType: "application/json",

dataType: "json",

I believe you need to set the contentType and dataType in your ajax request contentType: "application/json",

dataType: "json",

由于@ResponseBody意味着您的传递参数必须符合指定的格式,并且在您的ajax中,参数数据可能不符合格式 更改 public @ResponseBody String addToCart(@RequestBody ConfigurationForm

configurationForm, BindingResult bindingResult)

至 public @ResponseBody String addToCart(ConfigurationForm c

...

为什么分开呢? 尝试一下: content_types_accepted(Req, State) ->

Handler = [

{<>, handle_post_html},

{<>, handle_post_html},

{<>, handle_post_html}],

{Handler, Req, State}.

content_typ

...

固定。 我的代码没问题,这是JBoss Wildfly的错。 我将JBoss版本从wildfly-8.1.0.Final更改为wildfly-8.1.0.CR1然后就可以了! Fixed. My code is all right and it is the JBoss Wildfly's fault. I changed JBoss version from wildfly-8.1.0.Final to wildfly-8.1.0.CR1 then it works!!

它可能正在发生,因为你在运行时没有杰克逊在你的类路径上。 错误消息表明服务器由于某种原因无法处理您的JSON请求。 JSON被转换为Java对象,其中包含一个称为消息转换器的东西。 如果在Spring XML配置中有 (或者启用了Java Config),则会自动注册JSON消息转换器。 如果不这样做,则必须注册。 It may be happening because you do not have Jackson on your classpa

...