Case Study on line feed of the returned values of Ajax asynchronous data submission,
This article analyzes the newline problem of the returned values of Ajax asynchronous data submission. We will share this with you for your reference. The details are as follows:
The previous analysis introduced Javascript AJAX-based callback function Transfer Parameters and the difference between IE and FF about the Ajax parameter value transfer in Chinese. I learned how to handle Chinese garbled characters in different browsers by Using ajax callback function transfer. Here we will analyze how to handle the line feed problem in the return value of ajax asynchronous submission.
Background:
The function to be implemented is to select a category in the drop-down box and submit it to PHP to return the member name under the category according to the selected category. The name of each returned member is displayed as a line break to textarea.
Html code:
<Form action = "setduty. php? Action = add "method =" post "name =" setForm "id =" setForm "> <p> <label> type: </label> <select name = "type" id = "typeduty"> <option value = "-1"> select type </option> <option value = "1"> head of duty </option> <option value = "2"> head of duty </option> <option value = "3"> duty schedule </option> </select> </p> <p class = "opertext"> <label> member: </label> <textarea name = "names" id = "names" cols = "30" rows = "10"> </textarea> </p>
JQuery code:
$(document).ready(function(){ $("#typeduty").change(function(){ var type = $("#typeduty").val(); $.post("setduty_do.php", { 'cid' : type, },function(data,status){ newData = data.replace(/\r\n/g,''); $("#names").html(newData); }); });});
PHP code:
if($type){ switch($type){ case "1": $arrs = getChiefList(); break; case "2": $arrs = getDirectList(); break; case "3": $arrs = getAttendantList(); break; default: break; } $arr_names = ""; foreach($arrs as $arr){ if(strlen($arr[1])>2){ $arr_names.= $arr[1]."\n"; } } echo $arr_names;}
In php code, the returned values directly include the newline "\ n". In textarea, The newline characters of IE and FF are "\ n". In jQuery's return values, note that
Copy codeThe Code is as follows: newData = data. replace (/\ r \ n/g ,'');
Processing the returned results is used to remove the start line break symbol. All values returned by Ajax automatically carry a line break. This is what Ajax brings.
I hope this article will help you with ajax programming.
Articles you may be interested in:
- Analysis of data passing parameters in ajax
- An analysis of differences between IE and FF on the differences between parameters passed by Ajax in Chinese
- Example Analysis of passing parameters in Javascript Based on AJAX callback function
- Simple Ajax call instance implemented by jQuery + json
- Simple ajax call example
- Analysis of Ajax () data parameter type instances in JQuery