angular.js - The same execution request using $http will prompt a cross-domain request, but using jquery's ajax will be normal.
天蓬老师
天蓬老师 2017-05-15 17:00:18
0
2
566
$.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
        }
      })

Shouldn’t these two have exactly the same meaning? Why can it be executed successfully when using ajax, but when using $http, it prompts a cross-domain error?

天蓬老师
天蓬老师

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

reply all(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)

The method I found, the speed test was successful

滿天的星座

Angular uses jsonp cross-domain, of course, the premise is that your server returns jsonp format.

$http.jsop(url).success(fn).error(fn);
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template