responseText contains extra whitespace characters (new lines, line feeds), how to prevent and remove them?

前端 未结 6 551
忘了有多久
忘了有多久 2021-01-03 13:17

I have an ajax script that calls a php file.

The php file echos \"yes\" or \"no\", I want to use the strings to do logical comparisons.

In the javascript,

6条回答
  •  Happy的楠姐
    2021-01-03 13:33

    I think you may be going at it the wrong way. Instead of manually trying to create a response why don´t you use PHP arrays as a data structure and JSON for delivery?

     $flag)
    $json = json_encode($arr);
    
    // See http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp/
    // Set JSONP header
    header('content-type: application/json; charset=utf-8');
    
    // Get callback from $_GET and prepend the JSON data
    echo isset($_GET['callback'])
        ? "{$_GET['callback']}($json)"
        : $json;
    

提交回复
热议问题