解决ajax请求(SpringMVC后台)响应415/400/405错误

后端代码

  • bean


    public class user { private String username; private String password; private Integer age; //省略get set }
  • SpringMVC


    @RequestMapping(value = "testMethod",method = RequestMethod.POST) @ResponseBody public String getUser(@RequestBody user u){ System.out.println(u); return "success";
    }

415错误

没有设置 contentType: "application/json"

  • ajax

    $.ajax({
    
        url:baseurl+"test/testMethod",
    
         type:"POST",
    
        data:JSON.stringify({
    
            "username":"user666",
    
            "password":"a123456",
    
            "age":19
    
        }),
    success:function (res) { console.log(res); }, error:function () { console.log("网络错误"); } })
  • 原因


    //没有设置contentType: "application/json", //更正之后 $.ajax({ url:baseurl+"test/testMethod", type:"POST", contentType: "application/json", data:JSON.stringify({ "username":"user666", "password":"a123456", "age":19 }), success:function (res) { console.log(res); }, error:function () { console.log("网络错误"); } })

400错误

没有对传输的内容进行格式化 JSON.stringify(jsonobj)

  • ajax


    $.ajax({ url:baseurl+"test/testMethod", type:"POST", contentType: "application/json", data:{ "username":"user666", "password":"a123456", "age":19 }, success:function (res) { console.log(res); }, error:function () { console.log("网络错误"); } })
  • 原因


    //没有对传输的数据进行转换为JSON字符串JSON.stringify(jsonobj) //更正之后
    $.ajax({ url:baseurl+"test/testMethod", type:"POST", contentType: "application/json", data:JSON.stringify({ "username":"user666", "password":"a123456", "age":19 }), success:function (res) { console.log(res); }, error:function () { console.log("网络错误"); } })

405错误

通常是type 请求类型设置与后端所限制的请求不一致

  • ajax


    $.ajax({ url:baseurl+"test/testMethod", type:"GET", contentType: "application/json", data:JSON.stringify({ "username":"user666", "password":"a123456", "age":19 }), success:function (res) { console.log(res); }, error:function () {
    console.log("网络错误"); } })
  • 原因


    //请求类型与后端设置的不一致 正确应为 type:"POST" //更正之后 $.ajax({ url:baseurl+"test/testMethod", type:"POST", contentType: "application/json", data:JSON.stringify({ "username":"user666", "password":"a123456", "age":19 }), success:function (res) { console.log(res); }, error:function () { console.log("网络错误"); } })

解决ajax请求(SpringMVC后台)响应415/400/405错误的更多相关文章

  1. http 500 Internal Server Error的错误 ajax请求SpringMVC后台中返回500 Internal Server Error

    使用httprequester接口测试能返回数据,但是用ajax返回json格式的时候返回报500Internal Server Error. The server encountered an in ...

  2. 解决Ajax请求跨域问题

    from:https://blog.csdn.net/wang379275614/article/details/53333775 上篇文章提到,由于浏览器的同源策略,使得,AJAX请求只能发给同源的 ...

  3. Jquery progressbar通过Ajax请求获取后台进度演示

    项目源代码下载:http://download.csdn.net/detail/nuptboyzhb/6262253 1.简介 本文主要演示Jquery progressbar的进度条功能.js通过a ...

  4. 解决Ajax请求后台Servlet接口拿不到JSON数据问题

    前端Ajax请求代码如下: window.onload=function() { var url='http://127.0.0.1:8080/testpj/ErrorlogServlet'; $.a ...

  5. 使用ajax请求SpringMVC返回Json出现乱码解决方法

    1:在使用ajax请求后台访问数据的数据,后台返回的数据是乱码,带??问号的乱码,之前还一直没有遇到过,在这里记录整理一下,贴出解决代码! (1):前台使用ajax ,已经设定返回的结果为json格式 ...

  6. 解决ajax请求跨域

    跨域大部分需要通过后台解决,引起跨域的原因: 3个问题同时满足 才可能产生跨域问题,即跨域(协议,主机名,端口号中有一个不同就产生跨域) 下面是解决方法 方法一 // ajax请求跨域 /* *解决a ...

  7. 解决ajax请求cors跨域问题

    ”已阻止跨源请求:同源策略禁止读取位于 ***** 的远程资源.(原因:CORS 头缺少 'Access-Control-Allow-Origin').“ ”已阻止跨源请求:同源策略禁止读取位于 ** ...

  8. 解决Ajax请求时无法重定向的问题

    今天发现,当使用Ajax请求时,如果后台进行重定向到其他页面时是无法成功的,只能在浏览器地址栏输入才能够实现重定向. Ajax默认就是不支持重定向的,它是局部刷新,不重新加载页面. 需要实现的功能是, ...

  9. jQuery解决ajax请求的跨域问题

    这两天工作中频繁的遇到JS的跨域问题,都通过绕开ajax请求的方式.特地百度了一下,把跨域问题解决了.在这分析一下 首先贴上js的页面代码: <html> <head> < ...

随机推荐

  1. MySQL主从复制与读写分离概念及架构分析

    1.MySQL主从复制入门 首先,我们看一个图: 影响MySQL-A数据库的操作,在数据库执行后,都会写入本地的日志系统A中. 假设,实时的将变化了的日志系统中的数据库事件操作,在MYSQL-A的33 ...

  2. Codeforces Round #412 A Is it rated ?

    A. Is it rated? time limit per test  2 seconds memory limit per test  256 megabytes Is it rated? Her ...

  3. EditText的setInputType方法里面应该填什么?

    转自CSDN:http://blog.csdn.net/u014158743/article/details/52488010 | 以密码类型(password)为例 android:inputTyp ...

  4. switch case 忘记 break

    昨天纠结了很久的问题,一直找不到原因,早上发现是一个低级错误: private void selectButton(int id) { switch (id) { case 0: recommend_ ...

  5. androidtab

    https://github.com/H07000223/FlycoTabLayout tensorflow https://github.com/topics/tensorflow-examples ...

  6. Elk and nginx and redis 干货

    ELKStack ELKStack即Elasticsearch + Logstash + Kibana.日志监控和分析在保障业务稳定运行时,起到了很重要的作用.比如对nginx日志的监控分析,ngin ...

  7. GO语言(一)G语言自虐

    package main import . "fmt" //notice 1 type testInt func(uint32) bool func isOdd(integer u ...

  8. Linux 系统查看tomcat控制台命令

    前提进入tomcat/logs文件夹下 查看全部命令是:tail -f catalina.out 如果想查看具体文件的日志进入该文件所在目录然后命令如下: tail -f filename

  9. (转)对于ESP、EBP寄存器的理解

    原文地址https://blog.csdn.net/yeruby/article/details/39780943 esp是栈指针,是cpu机制决定的,push.pop指令会自动调整esp的值: eb ...

  10. 关于数据库插入sql操作速度的影响

    大概看了以下,适当多线程数据库连接操作比单线程效率高 多个sql语句组合后调用数据库连接执行比单个sql循环执行效率高的多 下面是几个参考资料,有空的时候详细整理一下 https://blog.csd ...