Angularjs JSONP不工作 [英] Angularjs JSONP not working

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

问题描述

我可能失去了一些东西,但我不能让这个JSONP要求的工作,这里是code:

I might be missing something here but I can't make this JSONP request work, here is the code:

var url =  'http://' + server + '?callback=JSON_CALLBACK';
$http.jsonp(url)
    .success(function(data){
       console.log('success');
    })
    .error(function () {
      console.log('error')
    });

请求触发确定,我得到的答复(与头内容类型:应用程序/ JSON)格式为:

The request fires ok and I am getting the response (with header Content-Type:application/json) in this format:

    [{"id": "1", "name": "John Doe"},
     {"id": "2", "name": "Lorem ipsum"},
     {"id": "3", "name": "Lorem ipsum"}]

你能看见什么错?也许我应该从服务器返回的格式是不正确的?
角无火灾除了我设置(错误)的错误信息错误回调。

Can you see anything wrong? Maybe the format I should return from the server is not right? Angular fires the error callback without any error message besides the one I set ('error').

推荐答案

JSONP 需要你做包装你的数据到JavaScript函数调用。所以,在技术上返回一个JavaScript文件,而不是一个JSON文件。

JSONP requires you do wrap your data into a JavaScript function call. So technically you return a JavaScript file and not a Json file.

从服务器返回的数据应该与此类似:

The data returned from server should similar to this:

// the name should match the one you add to the url
JSON_CALLBACK([
    {"id": "1", "name": "John Doe"},
    {"id": "2", "name": "Lorem ipsum"},
    {"id": "3", "name": "Lorem ipsum"}
]);

修改:如果另外一个人在遇到困难绊倒具有角的JSONP也请阅读这个问题,它包含有关如何角处理实际的回调函数有用的信息。

Edit: If some one else stumbles across problems with angular's JSONP please also read this answer to this question, it contains usefull informations about how angular handles the actual callback function.

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

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