angular.js - 同樣的執行請求使用 $http 會提示跨域,但使用 jquery 的 ajax 就正常
天蓬老师
天蓬老师 2017-05-15 17:00:18
0
2
572
$.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 就提示跨域了呢?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回覆(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);
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板