search

Home  >  Q&A  >  body text

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

天蓬老师天蓬老师2745 days ago550

reply all(2)I'll reply

  • 为情所困

    为情所困2017-05-15 17:02:18

    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

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-15 17:02:18

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

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

    reply
    0
  • Cancelreply