JavaScript backend directly returns Boolean type processing

Source: Internet
Author: User

R Recently used Jqueyr Ajax background verification to return a Boolean value directly to the foreground, using JSON format to upload to the foreground

var result = $.ajax ({url: '/' +window.location[' pathname '].split ('/') [1]+ '/resourceprivate/validateresourceprivate? Rid= ' +rid,async:false,datatype: "JSON"}). ResponseText;
Console.log (result);
The result is false.


Use directly in your code

if (!result) {console.log (result);}

There has been no output.

After debugging using Firebug, it turns out to be a string, so the following judgment will not be executed anyway.




All right, here we go. Just convert the result to a Boolean type, it's easy to think!

1. Use Boolean (result); The result is true

2. Using underscore's isboolean, the result is false


Another way to think about it: First use a string comparison and then judge

result = result== "false"? False:true;for the time being, is there any other way out? Finally, I think, this is using JSON, then use the jquery method to try

<pre code_snippet_id= "537953" snippet_file_name= "blog_20141201_4_5931945" name= "code" class= "JavaScript" > result = $.parsejson (result);
if (!result)
{
Console.log (result);
}


Perfect solution.


JavaScript backend directly returns Boolean type processing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.