简体   繁体   中英

POST request - String parameter is not present

I am using AJAX to send data to server. It works if I put all data to URL, so everything is OK. But when I put all data to "data" in AJAX, server writes - required String parameter is not present. Eventhough I can see all data in requests body (in browser). What might be wrong?

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);
          }
        });
      }

响应

Maybe the server is trying to read information through query string only, which means you should use GET method instead of a POST submission.

Try changing your method() function like this:

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);
      }
    });
}

And then, you call it without the json stringfy like this:

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

Can't say i am 100% on this answer but i have assumed 2 things here

That you are actually wanting to perform a 'GET' request here instead of post, as you have said you have tried the URL in the browser and then i assume what you mean is you have keyed this into the address bar then that would be a 'GET' request.

What you can use in JQuery is the $.params function to build your query. so then your code may look something like this.

Your Data object

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

Ajax method

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
   }
});

I think there will be a little work to do to map your values form where you are gettin them into your object and then also into the ajax method.

if you can give me more details about how these are pull together then i can happoly provide an update with more specific information

Hope this helps

NOTE if it is a POST of the data just need to understand what the payload would need to be like.

You are posting JSON:

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

You claim you are posting x-www-form-urlencoded data

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

If you are trying to post JSON, then send it with the correct Content-Type: application/json

If you are trying to post URL encoded data then:

  1. Send it with the correct Content-Type (it's the default for jQuery so omit the contentType parameter entirely, or get it correct ( application/x-www-form-urlencoded ).
  2. Send data in that format: state.search.method("POST", "users", data); (jQuery will encode an object in that format by default).

It works if I put all data to URL

If you want to post all the data in the URL, then you should be making a GET request and encoding it properly.

So:

Specify GET and, again, pass an object not a sting of JSON: state.search.method("GET", "users", data); (and jQuery will properly encode the data in the query string).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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