与JSONP角$资源不工作 [英] angular $resource with jsonp not working

查看:120
本文介绍了与JSONP角$资源不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有麻烦瓦特/以下code:

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' }
                }
            );
    });

调用Offer.query({},函数(){});在我的控制器工作没有任何问题。
但是这部分是不工作:

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);
        });

这总是返回400错误:

this always returns a 400 error:

NetworkError:400错误的请求 - <一个href=\"https://api.trustyou.com/hotels/d8421e79-99f0-41b2-8d6e-9cfd62a9776b/seal.json?callback=angular.callbacks._1\" rel=\"nofollow\">https://api.trustyou.com/hotels/d8421e79-99f0-41b2-8d6e-9cfd62a9776b/seal.json?callback=angular.callbacks._1\"

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

当我改变我的code和使用jQuerys.getJSON,我没有任何问题:

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);                           
        });

为什么jQuery的方法工作,但angulars $资源实现在这种情况下返回一个错误?

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

推荐答案

有一些问题,与棱角分明的回调函数,我的Git已经开放问题

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

回调的名称必须是JSONP_CALLBACK,其中角会变成回调名回调= angular.callbacks._1

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

有一些网络的Web服务不能接受angular.callbacks._1回调名。

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

解决方案:

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)

        });
    };


}​

我的小提琴 http://jsfiddle.net/pMGgR/

问题的关键是,你必须调用另一个JavaScript函数,让您的JSON输入反应。

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

希望这有助于

这篇关于与JSONP角$资源不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆