Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to send a HTTP GET request to a remote server and use the response on an HTML page. Below is the project/js/script.js page

    var app = angular.module('app', ['ngResource']);

    var config = {
        url:"www.myWebsite.com/discover",
        headers:  {
            "X-Object-Header" : "123456789 ",
            "Content-Type": "application/json"
        }
    };
    app.controller('discoverObjectCtrl', ['$scope', '$http', function (scope, http) {
        console.log('Everything Works!');

    http.get("/object", config).success(function (data) {
        scope.object = data;

    });
    console.log(scope.object);
}]);

In my response header, this is what I get

Remote Address:127.0.0.1:63342

Request URL:localhost:63342/object

Request Method:GET

Status Code:404 Not Found


Request Headers


Accept:application/json, text/plain, /

Accept-Encoding: gzip,deflate,sdch

Accept-Language:en-US,en;q=0.8

Cache-Control:max-age=0

Connection:keep-alive

Host:localhost:63342

Referer: localhost:63342/DemoSP/index.html

User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36

X-Object-Header: 123456789

What I would like to do is send the http request with a customized URL. So for instance I would like my console Header to display

Request URL:www.myWebsite.com/discover/object

And not

Request URL:localhost:63342/project/www.myWebsite.com/discover/object

Please I need Help on this. Thanks

share|improve this question

2 Answers 2

up vote 1 down vote accepted

You should configure the get like this:

$http.get("www.myWebsite.com/discover/object", {
    headers: {
        "X-Object-Header" : "123456789",
        "Content-Type": "application/json"
    }
}).success(...);

But you will run into CORS issues since the requested domain is not the same as the one where the current script is housed. You would either need to enable www.myWebsite.com to be queried in your server environment through the Access-Control-Allow-Origin headers or if you can change the www.myWebsite.com/discover/object endpoint then make it a JSONP endpoint which you can query through $http.jsonp.

See also this answer.

share|improve this answer

What you are actually doing is cross domain Ajax call. There are some typical solutions you can choose:

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.