繁体   English   中英

POST请求-字符串参数不存在

[英]POST request - String parameter is not present

我正在使用AJAX将数据发送到服务器。 如果我将所有数据都放入URL,它会起作用,因此一切正常。 但是,当我将所有数据放入AJAX中的“数据”时,服务器写入-所需的String参数不存在。 尽管我可以在请求正文中(在浏览器中)看到所有数据。 可能是什么问题?

const data = {
        firstName: name,
        surname: surname,
        email: email,
        password: pass1,
        roles: roles
      };
        state.search.method("POST", "users", JSON.stringify(data));

 method(type_, url_, data_){
        $.ajax({
          url: proxy + url_,
          type: type_,
          contentType: "x-www-form-urlencoded",
          dataType: "json",
          headers: {
            'Authorization': 'bearer ' + localStorage.access_token
          },
          data: data_,
          success: function(result){
              alert("OK METHOD"); 
          },
          error(XMLHttpRequest, textStatus, errorThrown){
            alert('Error: ' + errorThrown + ", " + textStatus);
          console.log(XMLHttpRequest.status + ' ' + 
              XMLHttpRequest.statusText);
          }
        });
      }

响应

也许服务器尝试仅通过查询字符串读取信息,这意味着您应该使用GET方法而不是POST提交。

尝试像这样更改method()函数:

method(type_, url_, data_){
    $.ajax({
      url: proxy + url_,
      type: "get", //Send using GET method
      headers: {
        'Authorization': 'bearer ' + localStorage.access_token
      },
      data: data_,
      success: function(result){
          alert("OK METHOD"); 
      },
      error(XMLHttpRequest, textStatus, errorThrown){
        alert('Error: ' + errorThrown + ", " + textStatus);
      console.log(XMLHttpRequest.status + ' ' + 
          XMLHttpRequest.statusText);
      }
    });
}

然后,您可以在使用json stringfy的情况下调用它,如下所示:

state.search.method("POST", "users", data);

不能说我对这个答案是100%,但是我在这里假设了2件事

您实际上是想在这里执行“ GET”请求而不是发布,就像您说的那样,您已经在浏览器中尝试了该URL,然后我假设您的意思是将其键入到地址栏中,那么那将是“ GET”请求。

您可以在JQuery中使用$ .params函数来构建查询。 因此您的代码可能看起来像这样。

您的数据对象

const data = {
            firstName: 'name',
            surname: 'surname',
            email: 'email',
            password: 'pass1',
            roles: 'roles'
        }

Ajax方法

let queryString = $.param(data)
let url = 'https://your base url';

$.ajax({
   url:url+'?'+queryString, //Building your url for getting the data
   method: 'GET',
   success: function(data){
     console.log(data);
   },
   error: function(jqXHR, textStatus, errorThrown){
     console.log(textStatus) // You can console.log the other values for debugging
   }
});

我认为需要做一些工作来将值形式映射到将其获取到对象中,然后再映射到ajax方法中。

如果您可以为我提供更多有关这些如何组合在一起的详细信息,那么我可以为您提供更具体的信息更新

希望这可以帮助

注意,如果它是数据的POST,则只需了解有效负载将是什么样子即可。

您正在发布JSON:

 state.search.method("POST", "users", JSON.stringify(data)); 

您声称您要发布x-www-form-urlencoded数据

 contentType: "x-www-form-urlencoded", 

如果您尝试发布JSON,请使用正确的Content-Type发送它: application/json

如果您尝试发布URL编码的数据,则:

  1. 发送正确的Content-Type(这是jQuery的默认设置,因此请完全省略contentType参数,或者将其正确设置( application/x-www-form-urlencoded )。
  2. 以这种格式发送数据: state.search.method("POST", "users", data); (默认情况下,jQuery将以该格式对对象进行编码)。

如果我将所有数据都放入URL,它会起作用

如果要发布URL中的所有数据,则应该发出GET请求并对其进行正确编码。

所以:

指定GET ,然后再次传递一个对象,而不是JSON形式: state.search.method("GET", "users", data); (并且jQuery会正确编码查询字符串中的数据)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM