I'm kind of new to angular, and it's the first time I'm using $http.put.
My problem is when I send the packet, it won't include the data (json object), and the PUT is a header(?).
This is what my code looks like:
app.controller('StatusController',['$http', function($http) {
this.updateComment = function (eventid, comment) {
var requestData = {"event-id":eventid, "new-comment":comment};
$http({
method: 'PUT',
url: "http://172.18.212.59/event",
data: requestData,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
});
}
}
So I used Fiddler to generate a PUT request to the server, and it worked. so I looked at the packets, trying to understand where the differences come from...
Here are the results:
Packet generated by Angular - Not working
Packet generated by the Fiddler Composer - Working
Also I've tried using:
$http.put(url, requestData)
But that didn't work either... My GET requests work perfectly fine! And they look like the packet that does work...
Has anyone encountered this problem before?
Appreciate your help!
OPTIONS verb is used when doing cross domain requests. Basically you need to enable cross domain requests on your server side code. The GET request works cause it isn't protected by cross domain restrictions and Fiddler works cause cross domain requests comes into picture when making XHR requests from browser. If you look into the response to the OPTIONS request you will probably find something saying cross domain request not allowed.
Related
I am invoking a service with $http protocol, but the url that I need to invoke is of https protocol.
As a result I am getting error with code : 403.
Snippet:
return $http({
method: 'POST',
url: 'https://test.com', /* sample url */
headers: some_header,
data: some_data
});
I have tried the same using HTML - form, and it worked there, so I don't think, it's an issue with the url.
I guess, this error is mainly because of $http.
How can one make https call in AngularJS using service or factory?
The 403 Forbidden error is an HTTP status code which means that
accessing the page or resource you were trying to reach is absolutely
forbidden for some reason. This article contains basic troubleshooting
instructions for 403 Forbidden errors.
source
Using Angular's $http service works with https. Here's a basic plunker doing exactly that. It retrieves a resource over https and updates $scope.results with the result.
$http.get('https://run.plnkr.co/plunks/77OOwJ/')
.then(function(data){
$scope.results = data;
})
I am trying to make an Angular application consuming Foreign Exchange (forex) market API.
$http({
method: 'GET',
url: 'http://www.apilayer.net/api/live?access_key=[MY ACCESS KEY]'
}).then(function(response){
console.log(response);
})
I keep getting the following error when I look at the browser console.
Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response.
The weirdest part is that it used to work - I was able to retrieve JSON info from the API call without a problem, but it does not any more.
Is there anyone who can point me to a set of possible causes? Would appreciate it!
If you didn't change anything in your code (say, the path for the api) then it must be one of two thing:
The browser configuration has changed
The api provider has blocked cross domain request, you should contact him, maybe he require callback with jsonp ajax request
I have a weird problem with AngularJS's $http service which, as far as I can see, no one else has.
Every time I use $http.post() to sent cross domain requests, I can see in the developer tools (network pane) that two different requests are being sent: one, without any data, and immediately after that another one is sent which has all the data and returns with the correct response from the server.
Here's an example:
$http.post(url+'getSiteList.php', {session_id: $scope.session_id(), withCredentials: true})
.success(function(data, status, headers, config) {
console.log(data);
....
Does anyone know what's causing this? I checked the code, and the $http.post method is only being called once.
It's all about how browsers manage CORS. When making a cross-domain request in angular, the browser will automatically make a HTTP OPTIONS request to the specified URL/URI it is called as "pre-flight" request or "promise".
As long as the remote source returns a HTTP status code of 200 and relevant details about what it will accept in the response headers, then the browser will go ahead with the original JavaScript call.
Im having a problem in getting the json result on one of my api's:
Tried calling it using the code below:
var url = 'domain.com/'
var query = $http({
method: 'GET',
url: url,
headers: {'Content-Type': 'application/json'}
});
console.log(query);
and a simple $http get on my controller
var url = $http.get('http://domain.com');
console.log(url);
Always gave me the
XMLHttpRequest cannot load http://domain.com. Origin domain is not allowed by Access-Control-Allow-Origin.
Then I tried changing the method type from GET to JSONP but gave me a
Uncaught SyntaxError: Unexpected token :
Weird coz if you view the url using a web browser it is a valid json object.
http://en.wikipedia.org/wiki/Cross-origin_resource_sharing
You are violating the single origin policy by trying to XHR data from a domain external to the domain that delivered the invoking JS.
Data retrieved with XHR must originate from the same host and port the site is served from.
This is put in place to protect against cross site scripting attacks.
You can work around this with JSONP (I suspect there is a seperate issue with your JSONP call, post it?) or by including the correct headers in the response from your API, Access-Control-Allow-Origin: *;
I am using Request Bin to post some data. In my controller, I have the following code:
$http.post('http://requestb.in/redacted', fooBar).
success(function(data) {
$scope.fooBarPostedSuccess = true;
}).
error(function(err) {
console.log("Error while posting to Request Bin");
console.log("Error Info : " + err);
});
This is triggered by means on a button on the UI. Now when this gets triggered, the data is not posted to Request Bin and I get this error:
XMLHttpRequest cannot load http://requestb.in/redacted.
Origin http://localhost:3000 is not allowed by Access-Control-Allow-Origin.
How do I post data to request bin through an AngularJS controller? Also, what does the above error mean?
EDIT : I wish to add here that I am using Node.js with AngularJS. Is this something to do with Node perhaps?
Ah yes... you are dealing with cross-domain scripting issues. This is not an AngularJS problem, but a browser security limitation and a VERY common friction point.
You cannot POST/PUT/DELETE to another domain (different from the one which is hosting -- localhost in your case) without doing some Cross-Origin Resource Sharing (CORS). You are limited to GET for a cross-domain HTTP request.
You have two options:
See if your API supports any cross-domain capabilities. This might be via CORS or it might be via an overloaded GET API or JSONP.
Proxy requests through your server. Since you are using Node.js, proxying REST through your server is extremely simple... you just need to set up a route handler (like /api/redacted) in your Node.js server and then make a new request to your actual API server with something like Restler (NPM package) and return the result back to your client.
Hope this helps!
EDIT:
Your API supports JSONP (Your API Docs). You should be able to use Angular's JSONP function to access your API's JSONP capabilities. (Angular.js JSONP docs).
Since you want to be able to POST to the service, you will need to use the second approach.
CORS allows both GET and POST
http://en.wikipedia.org/wiki/Cross-origin_resource_sharing
http://www.w3.org/TR/cors/
https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS
Now, that that's out of the way...
I too have found that angular's $http won't let me POST cross domain. I was suspicious about that though because I have jquery ajax calls in other projects that can post cross domain just fine. So, I swapped my $http POST with $.ajax POST and that worked.
// $http({
// url: url,
// method: "POST",
// data: data
// })
// .success(successCallback)
// .error(errorCallback)
// ole reliable
$.ajax({
type : "POST",
url : url,
data : data,
success : successCallback,
error : errorCallback,
cache : false,
dataType : 'json',
})
You can use PutsReq instead of RequestBin. PutsReq supports CORS.