简体   繁体   中英

How to check for empty json response with regex in javascript?

My ajax response returns an empty response (ie with multiple lines of blank spaces) and i want to check that in my success function. How to check for empty json response with regex in javascript?

> /^\s*$/.test("foo")
false
> /^\s*$/.test("")
true
> /^\s*$/.test("   ")
true
> /^\s*$/.test("\n\n")
true
  1. Set jQuery.ajax datatype to text
  2. Create the following success callback

     $.ajax({ // other options.. dataType: 'text', success(planinTextData) { if (planinTextData !== '') { var data = JSON.parse(planinTextData); } } }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM