简体   繁体   English

AJAX REST API请求

[英]AJAX REST API request

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. 我的问题是我无法以json行的形式向服务器发送正确的请求以从他那里得到答案。
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. XMLHttpRequest无法加载URL。 Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. 对预检请求的响应未通过访问控制检查:请求的资源上不存在“ Access-Control-Allow-Origin”标头。 Origin 'null' is therefore not allowed access. 因此,不允许访问原始“空”。

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. 参数已由dataSet和选项数据分配。 the allow origin header has to be set server side, not in the client 必须在服务器端而不是在客户端设置允许源标头

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

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