alert() does not work for ajax error function - javascript

Just want to alert a message when there are some unexpected errors(I changed 'controller/get_data' to 'controller/get_dat') but it does not alert anything. Could you please check what is wrong with my error handling function.
$.get('controller/get_data', function (data) {
if (data.message !== undefined) {
$( "#data_is_not_got").text(data.message);
} else {
//displaying posts
}
}, "json").error(function(jqXHR, textStatus, errorThrown){
alert("Something gone wrong. Please try again later.");
});

.error is not what you expect it to be. Furthermore, it's deprecated. What you want is .fail()
.fail(function(){
alert("Something gone wrong. Please try again later.");
});

I believe you need to try .fail instead of .error.
$.get('controller/get_data', function (data) {
if (data.message !== undefined) {
$( "#data_is_not_got").text(data.message);
} else {
//displaying posts
}
}, "json").fail(function() {
alert("Something gone wrong. Please try again later.");
});
Otherwise, you could use the more basic $.ajax.
$.ajax({
url: 'controller/get_data',
type: 'GET',
success: function(data){
// Do something with data
},
error: function(data) {
alert('Something's gone wrong!');
}
});

Related

Api call using Ajax in javascript giving error

i am getting error on calling ajax request with a code 422(unprocessible entity) , i dont understand what is wrong with my code can someone help me to figure out the error
$.ajax({
url: "/api/v3/email_exists",
method: 'POST',
data: {
email: email_entered
},
success: function (response, status) {
if (response.sent) {
showFlashMessage('Email Sent');
alert(response);
}
else {
showFlashMessage('An error occured, please try again latervrfg.', 'error');
}
},
error: function (response, status) {
showFlashMessage('An error occured, please try again later.', 'error');
}
});
url is returning either true or false in case of false it is giving status 422
Try with a full URL e,g https://Path_to _the controller_method

getJSON - working on response and displaying error message

I have a JSOn method as below:
$("#pwdOverride").change(function(){
alert("Inside onchange");
var pwdOverride = $("#pwdOverride").val();
alert("value of pwdOverride... "+pwdOverride);
$.getJSON(
'${pageContext.request.contextPath}/transaction/authenticationChk.do',
{paramFilter:'PROFILE',pwdOverride:pwdOverride},
function(data){
alert(JSON.stringify(data)); -> return would be "pwd" or "aPwd" or "err"
}
)
.success(function() { alert("second success"); })
.error(function() { alert("error"); })
.complete(function() { alert("complete"); });
});
JSOn response would be a String either "pwd" or "aPwd" else "err".
Based on the result I would want to display an inline error message similar to the validaiton rules.
like
messages: {
pwdOverride :"Incorrect pwd.",
cpwdOverride: "Incorrect sec pwd."
}
Is it possible? if so how do i achieve?

jQuery ajax error when sending data

I am trying to debug an error which is coming up when u run the following code. The ajax call simply carries a variable destroy which is a String. However, I am getting an error alert(xhr.status); which is return 0 and statusText is returning "error". Can anyone see a problem in this?
The new updated code is:
function logout() {
var destroy = "session_destroy"
FB.logout(function(response) {
$.post("setUser.php", { destroy: destroy }, function (data) {
alert(data);
window.location.href = "signup.php";
})
.fail(function() {
alert("error");
})
.always(function() {
alert("finished");
});
}
Note: This ajax calls intends to destory session whose code exists in setUser.php
I don't see any problem with your code. What is it the response setUser.php is giving you?
NOTE:
To see what is being logged in CHrome or IE >9 press F12, and go to console.
But why aren't you using the shorthand version?
$.post("setUser.php", { destroy: destroy }, function (data) {
alert(data);
window.location.href = "signup.php";
})
.fail(function(e, status, error) {
console.log(e);
console.log(status);
console.log(error);
alert(error);
})
.always(function() {
alert("finished");
});
DOCS

How to capture the 500 error message using jquery?

How can i capture the 500 error message using jquery? I want to keep on checking for the 500 error message for sometime until it changes and time out after 50 sec.
I used the code below to try to capture and check the 500 error message but it doesnt seem to catch the 500 error message. I can see it in the firebug
$.ajax({
statusCode: {
500: function() {
alert(" 500 data still loading");
console.log('500 ');
}
}
});
Dispite the accepted answer mentioned by #Danny, you can also do this in newer versions of jQuery.
var xhr = $.ajax({
url: "somewhere"
});
xhr.fail(function(xhr, textStatus, error) {
// Error handling stuff here ...
});
See Deferred Object.
Are you missing url in $.ajax like the one below
$.ajax({
url: "/path to page",
statusCode: {
500: function() {
alert(" 500 data still loading");
console.log('500 ');
}
}
});
You can check the status in error of ajax post please check the below code.
$.ajax({
.....
success: function (data) {
},
complete: function (XMLHttpRequest, textStatus) {
},
error: function (e, status) {
if (e.status == 404)
alert("404 error");
}
});
Thanks

getJSON: how to try to get some url until success in every 0.5 second?

So currently I use simple try once:
$.getJSON("server.json", function(data) {
servicesList.render(data);
});
I wonder how to try until success each 0.5 seconds?
function getFromServer(){
$.getJSON("server.json", function(data) {
// Verify your data, if it's not you want run error function
if(!data){ onerror(); return; }
servicesList.render(data);
}).error(function() { onerror(); });
}
// if error ,run it again.
function onerror(){
setTimeout(function(){getFromServer();},500);
}
you can find the $.getJSON API help with this link:
http://api.jquery.com/jQuery.getJSON/
Try this logic:
function update_it() {
$.ajax({
url: 'server.json',
dataType: 'json',
success: function(data) {
servicesList.render(data);
},
error: function() {
setTimeout('update_it', 500);
}
});
}
If fail, it waits 500 ms then recalls the function.

Categories

Resources