How to access the RESTful services from external server in my server in angularjs

老子叫甜甜 提交于 2019-12-12 01:03:14

问题


I am running on domain server1.

I have my RESTful services on server2.

I want to access this RESTful service on my webpage using angularjs. I tried this way. But it's returning an empty array. No data was bind. Why? any suggestions. Here is my sample code.


回答1:


In your sample code, your resource definition looks like this:

var UtilService = $resource('server2/users/:userId', { },
    {
        'get' : { method: 'GET',headers: {'Content-Type':'application/json'}, params: { userId:'anil' } , isArray : true }
    }
)

That URL appears to be relative. If you're trying to target a remote server, you're going to need a resolvable URI. Unless that's something that was mangled in the anonymizing of your example, I think that's your issue.




回答2:


You're having a similar problem to me I think. It looks like a SOP (same origin policy) issue. You can't access a different port on the local host without using JSONP or CORS. (I've got a question open with how to do this using CORS at the moment but there are plenty of docs available on the net for JSONP). see also this question

Hope this helps.



来源:https://stackoverflow.com/questions/16463456/how-to-access-the-restful-services-from-external-server-in-my-server-in-angularj

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!