Adding a port to $http

3,330 views
Skip to first unread message

Michael Bielski

unread,
Oct 24, 2012, 1:11:06 PM10/24/12
to ang...@googlegroups.com
I swear I've seen this somewhere before but my Google-Fu isn't awake this morning. When I look at the docs for $http (http://docs.angularjs.org/api/ng.$http) I don't see anything about how to handle a port other than the default. Am I able to do that with this?

var myPort = "8081";
var MC = $http({method:"GET", url: "myurl:port", port:myPort});

Pawel Kozlowski

unread,
Oct 24, 2012, 1:17:41 PM10/24/12
to ang...@googlegroups.com
Hi Michael!

I'm not sure, but you might be confusing $http usage with the
$resource usage. I mean, your syntax looks like you would be using
$resource but you are actually using $http.

I don't think you should have any problems with the port using $http,
just specify it as part of the URL. There are effectively some issues
with the port with $resource.

Cheers,
Pawel
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to
> angular+u...@googlegroups.com.
> Visit this group at http://groups.google.com/group/angular?hl=en.
>
>



--
Question? Send a fiddle
(http://jsfiddle.net/pkozlowski_opensource/Q2NpJ/) or a plunk
(http://plnkr.co/)
Need help with jsFiddle? Check this:
http://pkozlowskios.wordpress.com/2012/08/12/using-jsfiddle-with-angularjs/

Looking for UI widget library for AngularJS? Here you go:
http://angular-ui.github.com/

Michael Bielski

unread,
Oct 24, 2012, 1:47:13 PM10/24/12
to ang...@googlegroups.com
Thanks Pawel! I apparently need more sleep before I try to do this stuff!

Michael Bielski

unread,
Oct 31, 2012, 3:00:55 PM10/31/12
to ang...@googlegroups.com
Ok, this issue has arisen again, but in a somewhat different form. It appears that $http is having problems with URLs that have ports in them. Specifically:

//no port in url
$http({ method: "GET", url: "/v1/rest/services" });

//headers passed

GET /v1/rest/services HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0
Accept: application/json, text/plain, */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
X-Requested-With: XMLHttpRequest

//port in url
$http({ method: "GET", url: "/v1/rest/services:8081" });

//headers passed

OPTIONS /v1/rest/services HTTP/1.1
Host: localhost:8081
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
Access-Control-Request-Method: GET
Access-Control-Request-Headers: auth-session,x-requested-with

This is a problem. The method is not being used and the Accept header is not being passed. I need a work-around. Anybody have any ideas?

Tim Stewart

unread,
Nov 1, 2012, 2:12:57 AM11/1/12
to ang...@googlegroups.com
I'm surpised it works at all with the port number at the end of the URL, instead of directly after the host name. Try:


Or if your app page is also being served from localhost:8081, then you can just use:

$http.get("/v1/rest/services")

which is more generic (don't have to change it when serving app from elsewhere).

Tim

Paul Hammant

unread,
Nov 1, 2012, 9:09:40 AM11/1/12
to ang...@googlegroups.com
The same origin policy should always be in the back of our minds, should it not ?

Anecdote: I remember in 1997 making a Java applet for an insurance company, that shaped it's field according to parameters and accepted data entry for insurance quotes (cars, homes) and posted that to a SMTP service so that someone could contact the applicant later with a quoted.  Ahhh, them's the days.  My next step was ColdFusion for the same ... curiously underlining why I'm such a fan of AngularJS now.

-ph

--

Michael Bielski

unread,
Nov 1, 2012, 10:39:02 AM11/1/12
to ang...@googlegroups.com
It appears that you found a typo in what I copied/pasted into my post. Rest assured that the port number was in the right place (see the results for proof.) More specifically, the service that I am needing to reach listens on a specific port, and that port is not 80. There are other things on 80 that I interact with, and they work fine.
Reply all
Reply to author
Forward
0 new messages