同样的执行请求使用 $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 就提示跨域了呢?

阅读 3k
2 个回答
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)

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

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

$http.jsop(url).success(fn).error(fn);
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进