【发布时间】:2017-04-07 20:11:05
【问题描述】:
我有一个只有 2 个输入的表单。我想将 JSON 发送到我的 POST 方法。虽然,所有可能性都会返回此错误:
415 (Unsupported Media Type)
我试过用这 3 个 ajax:
console.log($("#idform").serializeArray());
console.log($("#idform").serialize());
var nome = "\"" + $("#idnome").val() + "\"";
var idade = "\"" + $("#ididade").val() + "\"";
var all = "{\n"+"\"name\": "+nome+",\n"+
"\"idade\": "+idade+"\n"+"}";
console.log(all.toString());
$.ajax({
url : 'http://localhost:8080/DBRest/rest/escreve',
type : "POST", // type of action POST || GET
dataType : 'json', // data type
data : all
})
$.ajax({
url : 'http://localhost:8080/DBRest/rest/escreve',
type : "POST", // type of action POST || GET
dataType : 'json', // data type
data : $("#idform").serializeArray()
})
$.ajax({
url : 'http://localhost:8080/DBRest/rest/escreve',
type : "POST", // type of action POST || GET
dataType : 'json', // data type
data : $("#idform").serialize()
})
这是我在控制台上打印后得到的结果:
nome=yrt&idade=09 //$("#idform").serialize()
{
"name": "yrt", //all
"idade": "09"
}
而$("#idform").serializeArray() 返回了[("name","yrt"),("idade","09")]
【问题讨论】:
-
您的服务器正在获取它不期望的数据,并返回
415。没有服务器端代码,就不可能说出原因
标签: javascript jquery json ajax