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

    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改