V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
haikea
V2EX  ›  问与答

小白请教个问题, ajaxq 请求总是报 400 错误

  •  
  •   haikea · 2018-04-11 00:20:04 +08:00 · 1688 次点击
    这是一个创建于 2201 天前的主题,其中的信息可能已经有所发展或是发生改变。
    贴上代码
    ---------------------后端代码-----------------------
    @RequestMapping(value="/testJson")
    @ResponseBody
    public TestJsonClass testJson(@RequestBody TestJsonClass testJsonClass){
    System.out.println("yyy");
    System.out.println(testJsonClass.getName());
    TestJsonClass jjj=new TestJsonClass("jjj");
    return jjj;

    }
    -------------------------TestJsonClass 类-----------------------
    public class TestJsonClass {

    private String name;

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public TestJsonClass(String name) {
    super();
    this.name = name;
    }
    }
    ----------------------------前端代码-------------------------------
    <!-- json&ajax 测试 -->
    <br><br>
    <form id="myForm">
    链接:<input type="text" id="name" name="name"/>
    <br>
    <input type="button" id="select" value="提交"/>
    </form>
    <script type="text/javascript">
    $("#select").click(function() {
    $.ajax({
    url : "testJson",
    type : "POST",
    contentType : "application/json;charset=UTF-8",
    "data": '{"name": "uname"}',
    success:function(result) {
    alert(result.name);
    }
    });
    });
    </script>
    haikea
        1
    haikea  
    OP
       2018-04-11 00:21:44 +08:00
    各位大神能帮忙看看是什么问题么
    Mazexal
        2
    Mazexal  
       2018-04-11 09:49:34 +08:00
    HTTP 错误 400
    400 请求出错
    由于语法格式有误,服务器无法理解此请求。不作修改,客户程序就无法重复此请求。
    q253382683
        3
    q253382683  
       2018-04-11 09:54:48 +08:00
    你这格式。。。
    quartet
        4
    quartet  
       2018-04-11 18:02:05 +08:00
    $.ajax({
    url : "testJson",
    type : "POST",
    contentType : "application/json;charset=UTF-8",
    dataType : "json",
    data: JSON.stringify({"name": "uname"}),
    success:function(result) {
    alert(result.name);
    }
    });
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2694 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 04:13 · PVG 12:13 · LAX 21:13 · JFK 00:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.