V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
haikea
V2EX  ›  问与答

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

  •  
  •   haikea · Apr 11, 2018 · 2682 views
    This topic created in 2938 days ago, the information mentioned may be changed or developed.
    贴上代码
    ---------------------后端代码-----------------------
    @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
       Apr 11, 2018
    各位大神能帮忙看看是什么问题么
    Mazexal
        2
    Mazexal  
       Apr 11, 2018
    HTTP 错误 400
    400 请求出错
    由于语法格式有误,服务器无法理解此请求。不作修改,客户程序就无法重复此请求。
    q253382683
        3
    q253382683  
       Apr 11, 2018
    你这格式。。。
    quartet
        4
    quartet  
       Apr 11, 2018
    $.ajax({
    url : "testJson",
    type : "POST",
    contentType : "application/json;charset=UTF-8",
    dataType : "json",
    data: JSON.stringify({"name": "uname"}),
    success:function(result) {
    alert(result.name);
    }
    });
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5709 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 09:01 · PVG 17:01 · LAX 02:01 · JFK 05:01
    ♥ Do have faith in what you're doing.