Directly return Boolean type processing in the javascript background

Source: Internet
Author: User

Directly return Boolean type processing in the javascript background

R recently used jqueyr's ajax background verification to directly return a Boolean value to the foreground and transmit it to the foreground in json format.

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 it directly in code

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

No output.

After debugging with firebug, it turns out to be a string, so the following judgment will not be executed in any way.




Well, here we only need to convert the result to the Boolean type. It's easy to think about it!

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

2. Use underscZ labels? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcmW1xGlzQm9vbGVhbiy94bn7ysdmYWxzZTxpbWcgYWx0PQ =" cry "src =" http://www.bkjia.com/uploads/allimg/141204/050522IW-1.gif ">


Try another method: first compare it with a string and then judge

Result = "false "? False: true; temporarily solved. Is there any other method? The last thought is that json is used in this example. Use the jquery method to try it.

result = $.parseJSON(result);
if(!result)
{
console.log(result);
}


Perfect solution.


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.