Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

javascript - angular $resource with jsonp not working

I'm having troubles w/ the following code:

angular.module('offerServices', ['ngResource'])
    .factory('Offer', function ($resource) {

        return $resource('url/offers', { callback: 'JSON_CALLBACK' },
            {
                query: { method: 'JSONP' }
            }
        );                   

    })
    .factory('Trustyou', function ($resource) {
            return $resource('https://api.trustyou.com/hotels/:id/seal.json', {},
                {
                    query: { method: 'JSONP' }
                }
            );
    });

calling Offer.query({}, function(){}); in my controller works without any problems. but this part is not working:

       var trustYouData = Trustyou.query({ id: 'd8421e79-99f0-41b2-8d6e-9cfd62a9776b' }, function (data) {
            console.log(data);
        });

this always returns a 400 error:

"NetworkError: 400 Bad Request - https://api.trustyou.com/hotels/d8421e79-99f0-41b2-8d6e-9cfd62a9776b/seal.json?callback=angular.callbacks._1"

when I change my code and use jQuerys.getJSON, I don't have any issues:

 $.getJSON("https://api.trustyou.com/hotels/d8421e79-99f0-41b2-8d6e-9cfd62a9776b/seal.json?callback=?", function (data) {
            console.log(data);                           
        });

Why is the jQuery method working but angulars $resource implementation returns an error in this case?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There's some issue with the callback function on angular, i've open an issue in git

https://github.com/angular/angular.js/issues/1551

The callback name must be "JSONP_CALLBACK" in which angular will turn the callback name to callback=angular.callbacks._1

There's some web web service which cannot accept "angular.callbacks._1 "callback name.

solution :

var stock_hack

function stock_search(data) {
    stock_hack = data;
 }


var stock_hack

function stock_search(data) {
    stock_hack = data;
}


function jsonp_example($scope, $http) {

    $scope.doRequest = function() {
        $http({
            method: "JSONP",
            params: {
                input: "GM",
                callback: "stock_search"
            },
            url: "http://dev.markitondemand.com/Api/Lookup/jsonp",
            isArray: true
        }).success(function(data, status) {
/*
                 *Never Goes HERE !!
                 */


        }).error(function(data, status) {

/*
                 * FREAKING HACK !!!!
                 */
            console.info("goes here")
            console.info(stock_hack)

        });
    };


}?

My fiddle http://jsfiddle.net/pMGgR/

The point is you must call another javascript function to get your json respone.

Hope this help


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...