Monday, 15 September 2014

javascript - How to compare result of the sucess function with a string when the dataType used to make ajax call to webservice is "jsonp" -



javascript - How to compare result of the sucess function with a string when the dataType used to make ajax call to webservice is "jsonp" -

i calling restful webservice using jquery

i want compare response returned success function string using if-else structure

note: response restful webservice jsonobject :- "welcome user"

here code:

$("#loginform").submit(function(event){ event.preventdefault(); var user = document.getelementbyid("username").value; var pass = document.getelementbyid("password").value; $.ajax({ url: "http://ec2-xx-xxx-xxx-xx.compute-1.amazonaws.com:8080/usermanagement/rest/user_details/sign_in", type: "post", crossdomain: true, datatype: 'jsonp', data: { username: user, password: pass}, success: function (result) { resultdiv.innerhtml=result; if (result.tostring() == "welcome user") { //link direct homepage } else alert("invalid details"); }, error: function (xhr, ajaxoptions, thrownerror) { } });

please @ if-else part , suggest me how compare response string?

if result of ajax phone call json object, won't able check deep equality way. need find key index of string value "welcome user".

if you're unsure of info coming in, can loop through json object , check "welcome user" in success callback :

var containedstring = false; (var key in results) { if (results[key] === "welcome user") { containedstring = true; } } if (containedstring = true) { // link homepage } else { alert("invalid details"); }

as side note, utilize triple === in javascript safer!

javascript jquery ajax web-services jsonp

No comments:

Post a Comment