问题
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