search

Home  >  Q&A  >  body text

angular.js - angularjs $resource发出的post请求如何详细设置?

angular的service是这样定义的(定义loginModule在另一个文件里,所以这里只是引用,没有写第二个参数):

angular.module('loginModule').factory('askSecCode',['$resource',
    function askSecCodeFactory($resource){
        return $resource('http://192.168.31.108:8080/retailer/user/auth',{},{crossOrigin:'Anonymous',userName:'QD100',userPass:'1234',checkCode:'1234'});
    }
]);

在controller里面是这样使用service的:

$scope.loginJump = function(info){
    askSecCode.save(
        {
            userName:info.staffID,
            userPass:info.password,
            checkCode:info.security
        },
        function(){
            console.log('post sent');
        }
    );
    //TODO: ??[?????????????????????][????]
    //TODO: ????????location.assign();
};

结果发出的请求如下:

而且请求结果是报错

我用jquery在同一位置请求是成功的,请求详情如下,和angular的请求确实不一样

求助:我如何让angularjs发出的请求,像jquery那样能被后端当作CORS跨域请求接受?

高洛峰高洛峰2743 days ago562

reply all(2)I'll reply

  • 迷茫

    迷茫2017-05-15 16:53:40

    Your problem is due to the inconsistent content-type in the header. The default in jquery is application/x-www-form-urlencoded, while in angular the default is application/json. If you want angular to make the same request as jquery, just change the header of resources

    reply
    0
  • PHP中文网

    PHP中文网2017-05-15 16:53:40

    Backend settings

    header('Access-Control-Allow-Origin:http://192.168.31.172:8000');

    reply
    0
  • Cancelreply