简体   繁体   中英

string delimiter ajax call

So a PHP file returns a string ( to an ajax call ) like this :

$output = $sessID."###".$sessEmail."###".$sessFirstName."###".$sessLanguage."###".$sessRememberMe;

and in javascript i do :

    if (reply.indexOf("###") >= 0) {
    arrayReply = reply.split("###");
    user.ID = arrayReply[0];
    user.Email = arrayReply[1];
    user.FirstName = arrayReply[2];
    user.Language = arrayReply[3];
    user.RememberMe = arrayReply[4];
    }

a problem can arise when parts of reply contain the the delimiter i use "###". What can I do in such a situation? Making the delimiter more complex/rare is not a solution in my opinion.

PS: I did try JSON but it's WAY SLOWER server side.

FINAL EDIT:

server side JSON is slower, and the same for client side, however it's not going to be a bottleneck ( 430ms for 100.000 calls ) and plus there is no need as Jules said below to re-invent the wheel. There was one more solution: bin2hex() in php [which reduced the time from 430ms to 240] and then get back the string in javascript with a hex2string function, however not worth the effort. JSON it is. Thank you all!

If as you say encoding as JSON is slower than you could try the following,

$output = '"' . some_kind_of_escape_function($sessID).'","'.some_kind_of_escape_function($sessEmail).'","'.some_kind_of_escape_function($sessFirstName).'","'.some_kind_of_escape_function($sessLanguage).'","'.$sessRememberMe.'"';

and of course replace some_kind_of_escape_function with the appropriate php function (eg addslashes or mysql_real_escape_string) it has been a while since I've done PHP development so choose the one that best suits your needs

Then it's a simple case of splitting by the comma and removing the quotes

One option is to use JSON object instead.

For PHP (using json_encode ):

$output = json_encode(array(
    "sessid" => $sessID,
    "sessEmail" => $sessEmail,
    "sessFirstName" => $sessFirstName,
    "sessLanguage" => $sessLanguage,
    "sessRememberMe" => $sessRememberMe
));

For JS (using jQuery method ):

$.getJSON("/path/to/script.php", function(reply) {
    user.ID = reply.sessid;
    user.Email = reply.sessEmail;
    user.FirstName = reply.sessFirstName;
    user.Language = reply.sessLanguage;
    user.RememberMe = reply.sessRememberMe;
});

Otherwise, you can use any other delimiter that possibly won't be found in the fields (or you can replace it throughout the fields). One of the examples is to use symbol of newline ( \\n ).

Why develop your own format if there is already one? use Json:

$output = json_encode(array('sessionID'=>$sessID,'sessionEmail'=>sessEmail,'sessionFirstName'=>$sessFirstName,'sessLanguage'=>$sessLanguage,'sessRememberMe'=>$sessRememberMe));

And for the Javsascript Side see http://www.javascriptkit.com/dhtmltutors/ajaxgetpost4.shtml or if your using JQuery etc. your Framework is much likely to have some kind of inbuild functionality such as http://api.jquery.com/jQuery.getJSON/

However if you want to use your ###-Delimiter i'd suggest you reduce it to just "#", for the sake of simplicity and space. After that introduce what is called an escape charater such as "\\" So in a prepass you'll parse your input and replace all occurences of # with #, vice versa in the output. You can then Split your String using a special Regex, which only splits by # and not by "#"

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