I am trying to dynamically add URL parameter to $httpbackend.whenGET
urlAll = "http://localhost:8080/api/Log";
urlById = "http://localhost:8080/api/Log?id=";
$httpBackend.whenGET(urlAll).respond(...); - This is Working fine
$httpBackend.whenGET(urlById).respond(...); - Not working
I append id dynamically to urlById when I call mock service. Since it is a dynamic value and the url is changing with dynamic id value, url is not matching with urlById (which is passed to httpbackend). Hence, throwing below exception.
Unexpected request: GET /http:\/\/localhost:8080\/api\/Log?id=/1e970422-a1a7-4ea5-9f74-1c84b53d7bc4
No more request expected
Gone through different posts and tried with regex. but ended up with below exception:
Unexpected request: GET /http:\/\/localhost:8080\/api\/Log\?id=.*/14f99a26-7850-4aa6-9953-db73fb1a1cab
Expected GET /http:\/\/localhost:8080\/api\/Log\?id=.*/.*/g
Any idea on how to add dynamical url to $httpbackend?
Most of the posts say the theoretical solutions and some posts given code changes, which didn't work. Any idea on what is wrong above?
Related
Can I (re)use Angular's $location or another Angular module to get URL parameters from a url? Or do I need to add (yet) another separate JS package to do this?
I have a URL string which I want to get the URL parameters from (aka searchstring). I know there's logic to do this in Angular.js, because $location has the search method. This returns the url parameters part as an object. For instance for a url https://www.domain.org/cool?minPrice=40&maxPrice=50 I can get the maxPrice value using: $location.search().maxPrice.
But this works only for the current url in the browser bar. I'm setting up an ngMock function that has to get url params from a URL passed in as a string parameter. I DON'T want a DIY solution as there is so much debate about what is correct, performant, etc due to things you might not think of it at first like:
- bad performance of regexp
- needing to url encode parameters
- order dependence, etc.
So I'd love to get this gift-wrapped. And ideally as an included-in-Angular solution so there's also no work for wrapping things up in an Angular service :P.
Note: If it's not possible I'll probably use uri.js, which I used to satisfaction in a non-Angular project a while ago.
I have some problems with jsonp and AngularJs
I one page, i have to make about 15 json calls, each one by jsonp
My calls look like this :
$http.jsonp("http://www.example.com/feed1.json?callback=JSON_CALLBACK1")
And in my json feed, the data is wrapped by JSON_CALLBACK1
But i always obtain this error :
Uncaught ReferenceError: JSON_CALLBACK1 is not defined
Now i noticed in the network tab in the Google Chrome inspector that the call was :
http://www.example.com/feed1.json?callback=angular.callbacks._0
So i changed my json file, to wrap data with angular.callbacks._0(); instead, and it worked for this one, but it did not work for all my json
I started wraping all my jsons with angular.callbacks._1, angular.callbacks._2, angular.callbacks._3 ... byt i noticed that the number isn't always the same ? and if the number is a two digits, it doesn't work. For example, i tryed angular.callbacks._15, and i always obtain this error
Uncaught ReferenceError: angular.callbacks._15 is not defined
So is there a way to fix all those jsonp problems once for all with AngularJs ?
Thanks
The callback=angular.callbacks._123 parameter is an information for the server that the client (browser) expects the response to be wrapped inside a function named angular.callbacks._123 like so:
angular.callbacks._123({
"key": "value"
});
You should change your server code to inspect the parameter value and if present use it as the wrapping function name.
You can find more details in wikipedia.
So I have the following angular 1.3 code
Restangular.one('user',270).get().then(function(existingUser){
existingUser.password = "foo";
existingUser.put();
});
Which grabs the user at http://api.dev/user/270 fine, however, the existingUser.put(); makes a PUT request to http://api.dev/user, ignoring the ID.
Changing to
Restangular.one('user/270').get().then(function(existingUser){
existingUser.password = "foo";
existingUser.put();
});
works fine, however looking at the examples on the Restangular homepage, it appears my original code should also work fine. Any pointers to whats going wrong?
Solved, issue was related to the existingUser being returned having a existingUser.userID attribute rather than a existingUser.id attribute.
Its this attribute that restangular seems to use for future post/put requests, rather than the id passed into the one() command.
I've been pulling my hair out trying to get the $http.json() to properly return a result set of objects. I have locally stored examples of the same response and they work fine.
Can anyone look at this code and tell me what I'm doing wrong? It has to do with returning $http or something in that bit.
Demo: http://plnkr.co/edit/x325wZ4mwi9DNM8tAxgH?p=preview
You seem to have changed your plunker. The first one where you were using $http.jsonp call seems close but with CORs issues it is hard to test - you should definitely check the encoding type of your JSONP returned data is correct. However...
I would structure the $http.get as per the code below. Now that you have dropped $http.jsonp in favour of $http.get, also note that you may now need to explicitly unpack the returned JSON data string into a javascript object via jsonDecode. Lastly you will likely have to unwrap your server response to return just the json payload with no JSON_CALLBACK() wrapper.
var url = 'http://sitesbyjoe.com/angular-tests/typeahead/schools.php?callback=JSON_CALLBACK'
$http.get(url).success(function(data) {
console.log(data);
$scope.schools = angular.jsonDecode(data);
});
My question is similar but not the same as this one.
Here is my fiddle
I'm working with a public resource. I don't think it is jsonp. (the flag they use is pjson which i think, to them , means pretty json).
If i have the method as JSONP it will call out and return but then i get an invalid label error. If i have the method set as GET i get the OPTIONS error in firebug (which i typically associate with cross-domain violations).
Oddly, my app calls out to other external resources without issue - so i'm not sure how it is getting that done and can't do this. Am I SOL if i have no control over this outside resource?
$scope.serviceDesc = layerRes.get();
It looks like you can make JSONP calls to this service you're using by specifying a callback=JSON_CALLBACK in the url parameters when using the $http service, or in your case the $resource service
Have a look at this example that I've written up: http://plnkr.co/edit/7EE85Mr8bZBUroQTp5A9?p=preview
$http.jsonp('http://services.arcgisonline.com/ArcGIS/rest/services/World_Physical_Map/MapServer/0?f=json&callback=JSON_CALLBACK')
.success(function(data) {
console.log('The data from their server:');
console.log(data);
$scope.worldPhysicalMap = data;
});
Converting this to use $resource shouldn't be much different.