Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何发送正确的AJAX JSON?_Javascript_Jquery_Json_Ajax - Fatal编程技术网

Javascript 如何发送正确的AJAX JSON?

Javascript 如何发送正确的AJAX JSON?,javascript,jquery,json,ajax,Javascript,Jquery,Json,Ajax,我有一个只有2个输入的表单。我想向我的POST方法发送一个JSON。尽管如此,所有的可能性都会返回此错误: 415 (Unsupported Media Type) 我尝试使用以下3种ajax: console.log($("#idform").serializeArray()); console.log($("#idform").serialize()); var nome = "\"" + $("#idnome").val() + "\"";

我有一个只有2个输入的表单。我想向我的POST方法发送一个JSON。尽管如此,所有的可能性都会返回此错误:

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()
返回
[(“名称”,“yrt”),(“idade”,“09”)]

您需要添加以下内容

contentType: 'application/json'
contentType是您发送到服务器的格式

$.ajax({
  url : 'http://localhost:8080/DBRest/rest/escreve',
  type : "POST", // type of action POST || GET
  contentType : 'application/json', // data type
  data : all           
})
数据类型是您希望从服务器返回的格式

$.ajax({
  url : 'http://localhost:8080/DBRest/rest/escreve',
  type : "POST", // type of action POST || GET
  contentType : 'application/json', // data type
  data : all           
})

您需要添加以下内容

contentType: 'application/json'
contentType是您发送到服务器的格式

$.ajax({
  url : 'http://localhost:8080/DBRest/rest/escreve',
  type : "POST", // type of action POST || GET
  contentType : 'application/json', // data type
  data : all           
})
数据类型是您希望从服务器返回的格式

$.ajax({
  url : 'http://localhost:8080/DBRest/rest/escreve',
  type : "POST", // type of action POST || GET
  contentType : 'application/json', // data type
  data : all           
})

您的服务器正在获取它不期望的数据,并返回
415
。如果没有服务器端代码,就不可能知道服务器的可能副本为什么会获得它不期望的数据,并返回
415
。如果没有服务器端代码,就不可能知道Good guess为什么可能重复,但是您如何知道服务器期望的是什么,OP没有告诉我们正在使用什么服务器端语言(尽管我猜是Java),这是一个很好的观点,但回答了他的问题,即如何发送正确的AJAXJSON@Zinc非常感谢你的耐心,这正是我的问题。很好的猜测,但是你如何知道服务器期望的是什么,OP没有告诉我们正在使用什么服务器端语言(尽管我猜是Java)很好,但是回答了他的问题,即如何发送正确的AJAXJSON@Zinc非常感谢你的耐心,那正是我的问题。