AJAX sends json data PUT to request other domain problems.

using node"s hapi framework, API service. Then request the API to have a cross-domain problem through ajax in a project.

route.options.cors
Default value: false (no CORS headers).

The Cross-Origin Resource Sharing protocol allows browsers to make cross-origin API calls. CORS is required by web applications running inside a browser which are loaded from a different domain than the API server. To enable, set cors to true

this is a hapi document. Open this to allow cross-domain requests from ajax.

in fact, it is possible to send ajax"s get request successfully. As shown in the figure

clipboard.png

PUT :

clipboard.png

MIME type application/json .

ajax

 

found that the request has been allowed.

returns 500 because my API accepts parsing by json. For example, the value of id under user is obtained by this.request.payload.User.id . But if the data is in x-www-from format, change it to this.request.payload.User [id] to get.

I want to know what went wrong with this put request to send json data that was not allowed.


cors option with additionalHeaders .

$.ajax({
  url: url,
  type: 'PUT',
  dataType: 'json',
  headers:{
    "authorization": tblMessageAPIServerAccessToken,
     contentType: "application/json",
  },
  data: params,
  success: function (res) {
    
  },
  error: function (res) {
    
  }
});

the correct way to write:

$.ajax({
  url: url,
  type: 'PUT',
  dataType: 'json',
  contentType: "application/json",
  headers:{
    "authorization": tblMessageAPIServerAccessToken,
  },
  data: params,
  success: function (res) {
    
  },
  error: function (res) {
    
  }
});

I put contentType in headers .

< hr >

but, in the actual test, putting it in headers will also have an impact on the data sent in the past.

Menu