我怎样才能插入前或prevent $ HTTP的JSONP在AngularJS自动解析命令?命令、HTTP、prevent、AngularJS

2023-09-13 02:52:17 作者:花开若相依

这多少每个问题或解释我找到有关好像pretty $ http或一般angularjs假定您可以修改您的请求的响应。我不能做到这一点,我得到的反应是(根据AngularJS解析器)畸形。它是畸形的以一致的方式,所以我可以修改纯文本解析之前,来解决这个问题,但(根据内容类型?)默认解析后的反应都拦截器和变换响应函数发生。

It seems like pretty much every question or explanation I find regarding $http or angularjs in general assumes you can modify the response from your requests. I can't do that and the response I'm getting is malformed (according to the AngularJS parser). It's malformed in a consistent way so I could modify the plain text to fix the problem before parsing it, but both response interceptors and transform response functions occur after the default (content type based?) parsing.

编辑:的问题是与事实,我需要使用JSONP方法,使从其他网站信息的请求,但数据并没有预期的JSONP回调等等东西(我如果根据内容或AngularJS code其浏览器)抛出一个语法错误:M还不能确定

The issue is with the fact that I need to use the JSONP methodology to make a request for information from another site, but the data does not have the expected JSONP callback so something (I'm still not sure if its the browser based on the content or the AngularJS code) throws a syntax error.

新问题:?有谁知道解决的办法

New question: Does anyone know a way around this?

推荐答案

这已经过测试,确实工作。让我知道,如果您有任何进一步的问题。 http://jsfiddle.net/moderndegree/Kn3Tc/

This has been tested and does work. Let me know if you have any further questions. http://jsfiddle.net/moderndegree/Kn3Tc/

HTML

<div ng-app="myApp">
    <div ng-controller="myController">
        {{results.tada}}
    </div>
</div>

的JavaScript

angular.module('myApp', ['ngResource']).
factory('myService', function($http, $resource, $log){
    return $resource('/', {}, {
        get: {
            method: 'GET',
            // placed custom transform ahead of $http default
            transformRequest: [function(data, headersGetter){
                $log.info(data);
                $log.info(headersGetter());
            }].concat($http.defaults.transformRequest),
            // placed custom transform ahead of $http default
            transformResponse: [function (data, headersGetter) {
                $log.info(data);
                $log.info(headersGetter());
                data = {tada:"Check your console"};
                return data;
            }].concat($http.defaults.transformResponse)
        }
    });
}).
controller('myController', function(myService, $scope) {
    $scope.results = myService.get();
});

更新要使用JSONP,只需切换方法JSONP。你可以阅读更多关于ngResource 这里。