【问题标题】:React.JS POST value with XMLHttpRequest in windowsReact.JS POST 值与 Windows 中的 XMLHttpRequest
【发布时间】:2019-07-29 06:08:29
【问题描述】:

我只是想使用 rest API 存储一些值。

[{"userID":1,"passwordHash":"asdasd","email":"admin@admin.com"},{"userID":2,"passwordHash":"12345","email":"admin@admin.com"}]

这就是我的数据的样子。

我在 Windows 机器的服务器端使用 Spring Boot。要发布价值,我在控制台中使用它:

curl -X POST -H "Content-Type: application/json" -d "{ \"email\": \"aszzzzzzzzzzd\", \"passwordHash\": \"sad\" }" http://localhost:8080/user

现在我正在尝试从我的 react.js 应用程序中POST。所以我使用XMLHttpRequest()

这是我的 react.js 应用程序的代码:

    var xhr = new XMLHttpRequest();
    xhr.open('POST', 'http://localhost:8080/user');
    xhr.send(JSON.stringify({ email: this.state.email,passwordHash:this.state.password }));

我已添加 @CrossOrigin(origins = "http://localhost:3000") 以接收来自我的应用的请求。

但问题是,它给了我一个错误提示:

发布http://localhost:8080/user415

【问题讨论】:

    标签: javascript reactjs spring-boot xmlhttprequest httprequest


    【解决方案1】:

    请求标头格式不正确,需要将信息作为值发送:

    xhr.setRequestHeader("Content-Type", "application/json");
    

    【讨论】:

      猜你喜欢
      • 2020-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-22
      • 2011-05-15
      • 2011-11-06
      • 1970-01-01
      • 2022-01-17
      相关资源
      最近更新 更多