weixin_33717298 2017-04-13 06:35 采纳率: 0%
浏览 134

AJAX REST API请求

My problem is that I can not send the correct request to the server to get an answer from him in the form of a json line.
Here is my code:

$.ajax({
    type: "GET",
    url: url,
    dataType: 'json',
    crossDomain: true,
    username: 'username',
    password: 'password',
    headers: {
        'Access-Control-Allow-Origin': '*'
    },
    success: function(response) {
        console.log(response);
    }
})

When I try to send a request, I get an error:

XMLHttpRequest cannot load URL. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

  • 写回答

1条回答 默认 最新

  • from.. 2017-04-13 06:59
    关注
    var dataSet = {
      username: 'username',
      password: 'password',
    }
    
    $.ajax({
      type: "GET",
      url: url,
      dataType: 'json',
      crossDomain: true,
      data: dataSet,
      // the origin header has to be set server side
      /* 
      headers: {
       'Access-Control-Allow-Origin': '*'
      },
      */
      success: function(response) {
        console.log(response);
      }
    });
    

    parameters have assigned by a dataSet and the option data. the allow origin header has to be set server side, not in the client

    评论

报告相同问题?