getJSON - working on response and displaying error message - javascript

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?

Related

jQuery to read json data

I am trying to get jquery to read json data but getting undefined.
$.post( wce_data.url, { userid : userId, token : accessToken }, 'json')
.done(function( data ) {
console.log("data.status: " + data.status);
if (data.status === "success") {
alert("Coupon added");
} else {
alert("failed");
}
})
.fail(function() {
alert("The requested action failed. Please contact the site administrator!");
});
data.status: returns 'undefined'
data: returns {"status":"success"}
Try error checking like this:
.done(function() {
alert( "Coupon added" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "finished" );
Oh I see. I got it. It was the PHP Output Header that was needed.
I added:
header("Content-type: application/json");
To the PHP for jQuery.
Hope this helped someone new to json like me.

Issue parsing json from jquery

I'm new to jquery I have a php that returns a json, so I can get it from jquery, but there is a problem getting the result.
Here's my code:
calculate: function(me, answer, res_id, soulmates) {
console.log('CALCULATE: ');
var deferred = $.Deferred();
data = {
'me': me,
'answer': answer,
'resid': res_id,
};
$.ajax({
url: appConfig.calculate_url,
type: 'post',
beforeSend: function() {
console.log('BEFORE');
Site.switch_calculation_animations();
console.log('AFTER');
console.log(appConfig.calculate_url);
},
data: JSON.stringify(data),
timeout: 15000
}).done(function(ans) {
console.log(ans);
console.log(ans.ok);
console.log(ans.combi_id);
console.log(ans.slug);
if (ans.ok == 'yes') {
console.log('YES');
deferred.resolve(ans);
}
}).fail(function(jqXHR, textStatus, error) {
console.log('ERROR');
Site.handle_exception('calculate', {
'textStatus': textStatus,
'error': error
});
deferred.reject();
});
console.log('END CALCULATE');
return deferred.promise();
},
The console log I get is:
CALCULATE:
app.js?v=35:242 BEFORE
app.js?v=35:244 AFTER
app.js?v=35:245 /es/test_calculate/4170/waiting/
app.js?v=35:266 END CALCULATE
app.js?v=35:250 {"ok":"yes","combi_id":6059244666,"slug":"true"}
app.js?v=35:251 undefined
app.js?v=35:252 undefined
app.js?v=35:253 undefined
So although the ok value is "yes", do not enter into the if command. Why? What I'm missing?
Thanks
Try this:
}).done(function(ans) {
var data = $.parseJSON(ans)
console.log(data);
console.log(data.ok);
console.log(data.combi_id);
console.log(data.slug);
if (data.ok == 'yes') {
console.log('YES');
deferred.resolve(data);
}

JQuery post request failing without alert after post

I have post request
var prodId = getParameterByName('param');
var pass = $('#password1').val();
$.post("rest/forget/confirm", {
"param" : prodId,
"password" : pass
}, function(data) {
alert(data);
}).done(function() {
alert("second success");
}).fail(function() {
alert("error");
}).always(function() {
alert("finished");
});
});
I obtain in alert "error" and "finished" but if i add alert("lala"); code:
var prodId = getParameterByName('param');
var pass = $('#password1').val();
$.post("rest/forget/confirm", {
"param" : prodId,
"password" : pass
}, function(data) {
alert(data);
}).done(function() {
alert("second success");
}).fail(function() {
alert("error");
}).always(function() {
alert("finished");
});
alert("lala");
});
in alert i obtain "lala" after my data then "second success" and "finished". Also if i debug this function without alert it works too.
why i get error in alert is missing, how i get successful without alert?
Add property async :false in your ajax call.

alert() does not work for ajax error function

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!');
}
});

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

Categories

Resources