Success callback on $http.post in AngularJS using Firefox - ajax

I have a simple CORS AJAX call from within AngularJS application with success callback:
$http({method:'POST',url:"http://0.0.0.0:4567/authenticate",
params: {Lusername:scope.Lusername,Lpassword:scope.Lpassword}})
.success(function(){alert("Success")})
When used in Safari it works fine: returns expected JSON object and shows the alert box. However in Firefox, although the JSON object is returned properly the success callback is not triggered.
Any idea why?

Make sure you handle the OPTIONS request in the server. If it returns 404 then Firefox wont call the next request (in your case the POST mentioned above).

Try this with last version of AngularJS:
$http.post("http://0.0.0.0:4567/authenticate", {
Lusername: $scope.Lusername,
Lpassword: $scope.Lpassword
}).success(function(data, status, headers, config) {
alert("Success");
});

Related

In ie11 ajax call -window.location redirect not working, extjs

I am facing an issue with redirecting to another domain, from ajax call success in ie. It shows issue only in ie, in chrome and firebox it works well. I am using extjs for ajax call and in response i want to redirect to another domain based on the success value.
But if i place an javascript alert in the ajax success it works in ie.
If we place the same redirection code outside success of ajax call it works.
Ext.Ajax.request({
url: 'API/LogOut.ashx',
scope: this,
success: function (response) {
var result = response.result.data;
if (result['LogOutUrl'])
{
window.location.assign(result['LogOutUrl']);
I even tried to redirect like this window.location.assign('/tax.aspx'). But even this is not working. Anybody has any clue on that.
I also faced same issue for IE browser, in IE due to security it blocks window redirection like window.open().It results in popup blocker.
Try using delayed task or setTimeout() after your ajax response comes.
Ext.Ajax.request({
url: 'API/LogOut.ashx',
scope: this,
success: function (response) {
var result = response.result.data;
if (result['LogOutUrl'])
{
setTimeout(function(){
window.location.assign(result['LogOutUrl']);
},1000);
...
Post your result here.If possible give some fiddle link.

How to get/post/delete/put information with jQuery and AJAX

I trying to do a DELETE, PUT, GET and POST a request with ajax and jquery.
The method POST works well by creating a new record, but I cannot make it work the other methods (PUT, DELETE and GET).
This is the code (it works fine, it creates the new record but it doesn't reach the "success" event):
var jsonExample = {"advertisement":{"title":"test"}};
$.ajax({
type: "POST",
url: "http://example.com/advertisements.json",
data:jsonExample,
success: function(response){
alert("test");
}
});
When I change the type "POST" to "DELETE" or "PUT" I have the follow error:
NetworkError: 404 Not Found
And when I change it to "GET" it throws the following message:
200 OK
But it don't any other responses. It should be something like this:
{"advertisement":{"created_at":"2012-04-17T13:20:17Z","from_age":null,"neighbourhood_id":null,"title":null,"date_to":null,"days":null,"promotion_id":null,"updated_at":"2012-04-17T13:20:17Z","date_from":null,"gender":null,"id":3,"display":null,"desc":null,"budget":null,"image":null,"to_age":null,"department_id":null,"town_id":null}}
The
Please note: my app is getting this info from a remote server, but I don't know if that has something to do with this problem. Because I've run it in Google Chrome and I've received the Access-Control-Allow-Origin message on the browser's console.
Any ideas?
You cannot make cross-domain AJAX requests using jQuery for security reasons. You may however be able to use jsonp providing that the URL you are requesting the data from is set up to handle jsonp requests.
This article should help you out alot more than I'm able to: http://www.fbloggs.com/2010/07/09/how-to-access-cross-domain-data-with-ajax-using-jsonp-jquery-and-php/

Capturing Errors in Flexigrid Ajax Requests

I'm creating a webApp in which I show some table data using jQuery and the Flexigrid plugin. In the ideal scenario everything goes ok but I when I return a server-side exception or a validation error on a Flexigrid's ajax request, it does nothing but keep the loading icon and the 'Processing, please wait' message.
Right now you can see this behavior in its sample page, in which the server returns a 404 error but the flexigrid appears like its still loading but never shows any error.
Is there any event I could capture and handle Flexigrid's ajax request errors?
$('#flex').flexigrid({
onError: function(data){
// do stuff here
}
});

jQuery and Ajax with json - fails in IE

I'm using jQuery (1.7.0) to make a json/ajax call to Spotify. The following code works fine in Chrome and Firefox, but causes an error (Error: Access is denied.) in IE.
$.ajax({
url: 'http://ws.spotify.com/lookup/1/.json',
type: 'GET',
dataType: 'json',
cache: true,
data: {
uri: "someartist",
extras: "album"
},
success: successfn,
error:function(xhr, status, errorThrown) {
alert("networking error: "+errorThrown+'\n'+status+'\n'+xhr.statusText);
}
});
The success function is called in Chrome and FF, but the error function is called in IE with the above message. I have set cors to true: jQuery.support.cors = true;.
It works on Chrome and FF both locally and on my server, it works in IE locally but not on the server. Changing cache: false causes problems at the spotify end - doesn't line additional parameters, so I get a "bad request" error.
Grateful for any pointers.
Thanks
Abo
You are relying on the spotify url to give a Access-Control-Allow-Origin:* in their header to allow cross domain requests from all domains. Internet explorer however doesn't support this, so it gives access denied.
access-control-allow-origin explained. (TLDR: Servers may allow cross domain ajax in their headers)
If you need this to work in IE, you could use spotify's JSONP API if they have one or make the AJAX request in flash, which works in all browsers and passes the requests response data to your javascript.
The above answer about using jsonp is correct; I want to add:
Don't set
jquery.support.cors = true;
I'm not sure why so many questions begin by stating they took that step. This property is meant to be read to find out if the browser supports CORS. You should only override it if you know differently, and in my experience it's accurate for all major browsers. Setting it to true doesn't enable the browser to use CORS, it just denies you the info that CORS is going to fail.
http://api.jquery.com/jQuery.support/
can you give an example of returned data?
at a /guess/, it either has something to do with the filename ".json", or the JSON returned has something weird about it.
I'm surprised this works on Chrome or Firefox. You shouldn't be able to run cross-domain JSON requests.
If Spotify API supports it, you should use JSONP in order to access resources from other domains.
Also see: No response from jQuery ajax call
I don't see this working in FF. You can't make cross-domain Ajax calls. So I'm not sure what's going on when you say that it works in FF. But I just tried the following in FF and I got the error. So all you can do is make the call on the server side and then include the results in your page.
http://jsfiddle.net/2XWGn/

My Ajax request throws an error in django, how do I find what it is?

How do I get more information about the errors in my Ajax requests?
When error occurs in Django during a normal HTTP request, Django shows a descriptive error page with exception details.
However, when error occurs during an AJAX request, I just know the HTTP error code and that's it. I'd like to know as much details as I learn from a Django error page.
Handle errors within your ajax request and use responseText to get the response text and put it into an empty div that you have created for that.
For example:
$.ajax({
url:"{% yourView %}",
dataType: 'json',
success: function(jsonData){
alert("Hooray!");
},
error: function(jqXHR, textStatus, errorThrown){
$('.errors_div').html(jqXHR.responseText);
}
});
The django error page will be rendered into div class=errors_div
Your web server's error log will contain additional information about any uncaught exceptions.
Use Firebug. In Console you'll see the AJAX request, just right click -> Copy Response Content and save that as an HTML file.
Why not access the url directly in your browser's address bar so you see a normal traceback?
Wrap it in another view to change the MIME type to text/html.
I also often have a view that wraps the JSON output in a html tag so the Django Debug Toolbar works correctly.

Resources