首页 > 同样的执行请求使用 $http 会提示跨域,但使用 jquery 的 ajax 就正常

同样的执行请求使用 $http 会提示跨域,但使用 jquery 的 ajax 就正常

$.ajax({
          url: api.regist,
          type: 'POST',
          dataType: 'json',
          data: {
            email: $scope.email,
            password: $scope.password
          }
        })
$http({
        url: api.regist,
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        data: {
          email: $scope.email,
          password: $scope.password
        }
      })

这两个不应该是完全一样的意义嘛?为什么使用 ajax 可以执行成功,但是使用 $http 就提示跨域了呢?


angular跨域使用 jsonp,当然前提是你的服务器返回的是 jsonp 格式。

$http.jsop(url).success(fn).error(fn);

postCfg = {
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
  },
  transformRequest: function(data) {
    return $.param(data);
  }
}

app.config([
  '$httpProvider',
  function($httpProvider) {
    $httpProvider.defaults.useXDomain = true;
    delete $httpProvider.defaults.headers.common['X-Requested-With'];
   }
]}

$http.post(url, {}, postCfg)

自己找到的方法,测速成功

【热门文章】
【热门文章】