search

Home  >  Q&A  >  body text

javascript - Please help, angularjs. I can request json data but it reports a cross-domain problem. I can open the json link given to me by the backend. Here is my request file.

// angularjs initialization ng-app="myApp"

var app = angular.module('myApp',[]);
app.controller('myCtrl',function($scope,httpService){
    var obj = {
        'v' : 1,
        'prod_id' : 10,
        't' : 0.6861832864488719,
        'query' : print
    };
    httpService.postDatas('http://sys.hannikang.com/prod/process/prod.ashx?v=1&prod_id=10&t=0.6861832864488719&query=print',obj,function(data){
        console.log(data);
        $scope.names = data;
    })
});
app.service("httpService", function ($http) {
    return {
        getDatas: function (url, obj1, succCallBack, errorCallBack) {
            return $http({
                method: "GET",
                url: url,
                params: obj1 || {}
            }).success(function (data) {
                succCallBack && succCallBack(data);
            }).error(function (data) {
                errorCallBack && errorCallBack(data);
            })
        },
        postDatas: function (url, obj1, succCallBack, errorCallBack) {
            return $http({
                method: "POST",
                url: 'http://sys.hannikang.com/prod/process/prod.ashx?v=1&prod_id=10&t=0.6861832864488719&query=print',
                data: obj1 || {},
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                transformRequest: function (obj) {
                    var str = [];
                    for (var p in obj) {
                        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                    }
                    return str.join("&");
                }
            }).success(function (data) {
                succCallBack && succCallBack(data);
            }).error(function (data) {
                errorCallBack && errorCallBack(data);
            })
        }
    }
});
伊谢尔伦伊谢尔伦2857 days ago338

reply all(5)I'll reply

  • 阿神

    阿神2017-05-16 13:01:40

    Modify service to

    getDatas: function (url, obj) {
                return $http({
                    method: "GET",
                    url: url,
                    params: obj || {}
                })
                })
            

    When called

     httpService.postDatas('http://sys.hannikang.com/prod/process/prod.ashx?v=1&prod_id=10&t=0.6861832864488719&query=print',obj={})
     .then(function(data){
     console.log(data)
     })

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-16 13:01:40

    Allow domain failure through server settings (the server where the front-end files are located)
    For example, nginx is set as follows

    if ($request_method = 'OPTIONS') {

    add_header Access-Control-Allow-Origin *; 
    add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;
    #其他头部信息配置,省略...
    return 204; 

    }

    Other web servers or back-end languages ​​such as php have setting methods, check the following configuration items

    Access-Control-Allow-Origin

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-16 13:01:40

    Let the backend add a same-origin filter

    resp.setHeader("Access-Control-Allow-Origin", "*");  
    chain.doFilter(req, resp);

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-16 13:01:40

    I downloaded a local server. My company has a local server. I don’t know if this will affect it;

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-16 13:01:40

    chrome-extension://nlfbmbojpeacfghkpbjhddihlkkiljbi/options.html

    Use chrome download proxy tool

    Then just configure the address of your server. That’s what I did. I’m too lazy to deal with the backend

    reply
    0
  • Cancelreply