john
john

Reputation: 11

compare string obtained via ajax

I am using jquery:

    $.get(URL, function(data){

            if(data == 'no_credits'){
                alert("no credits");
                return;
            }
            else{
                // WHY HERE>?!
            }

and my problem is that url return 'no_credits' but code goes to else block code, please help!

EDIT : alert(data); prints no_credits

Upvotes: 1

Views: 1191

Answers (4)

Chintoo0
Chintoo0

Reputation: 1

to send data to front end from backend code....make sure u r not using any function which adds something by itself...like println() function of Java Streams adds one "\n" at the end of string and then sends..use print() function instead...

Upvotes: -1

Nabab
Nabab

Reputation: 2644

Try this:

$.get(URL,function(data){...},'text');

Your function is outside the $.get function, so of course it doesn't get the server's response as argument.

Upvotes: 1

Mihai Toader
Mihai Toader

Reputation: 12243

Have you tried dumping the data object ? do a console.log(data); in Firefox (with installed firebug) and check the firebug console. I think the data is not the output of the server but a response object.

Upvotes: 0

SLaks
SLaks

Reputation: 887449

You probably have trailing spaces or capitalization differences.

Try

if($.trim(data).toLowerCase() === 'no_credits'){

Upvotes: 3

Related Questions