0

I have a site and I used AJAX. And I got some problems.

Server return JSON string something like this {a:"x48\x65\x6C\x6C\x6F"}.

Then in xx.responseText, we have this string '{a:"\x48\x65\x6C\x6C\x6F"}'.

But if I create JavaScript string "\x48\x65\x6C\x6C\x6F" then I have "Hello" and not HEX!

Is it possible get in xx.responseText "real" text from HEX (automatically, without .replace())?

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Ralo
  • 59
  • 6
  • Do you have access to the server? If so, then let the script output the real Hex code. – ComFreek Sep 12 '13 at 17:41
  • possible duplicate of [Decoding hex string in javascript](http://stackoverflow.com/questions/4209104/decoding-hex-string-in-javascript) – BenLanc Sep 12 '13 at 17:54

2 Answers2

2

If the output is at all regular (predictable), .replace() is probably the simplest.

var escapeSequences = xx.responseText.replace(/^\{a:/, '').replace(/\}$/, '');

console.log(escapeSequences === "\"\\x48\\x65\\x6C\\x6C\\x6F\""); // true

Or, if a string literal that's equivalent in value but may not otherwise be the same is sufficient, you could parse (see below) and then stringify() an individual property.

console.log(JSON.stringify(data.a) === "\"Hello\""); // true

Otherwise, you'll likely need to run responseText through a lexer to tokenize it and retrieve the literal from that. JavaScript doesn't include an option for this separate from parsing/evaluating, so you'll need to find a library for this.

"Lexer written in JavaScript?" may be a good place to start for that.


To parse it:

Since it appears to be a string of code, you'll likely have to use eval().

var data = eval('(' + xx.responseText + ')');

console.log(data.a); // Hello

Note: The parenthesis make sure {...} is evaluated as an Object literal rather than as a block.


Also, I'd suggest looking into alternatives to code for communicating data like this.

A common option is JSON, which takes its syntax from JavaScript, but uses a rather strict subset. It doesn't allow functions or other potentially problematic code to be included.

var data = JSON.parse(xx.responseText);

console.log(data.a); // Hello

Visiting JSON.org, you should be able to find a reference or library for the choice of server-side language to output JSON.

{ "a": "Hello" }
Community
  • 1
  • 1
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
  • How it work, i mean xmlhttp parse response. Because i send only one backslash \x22 and XHR return string like '\x22' what is equivavelent the string '\\x22' I read w3c specification, and i know about utfdecoder http://www.w3.org/TR/XMLHttpRequest/#text-response-entity-body But i cant find it, cant find how XHR parse backslashes, how not parse backslash? I want to get real value in responseText =( – Ralo Sep 12 '13 at 18:24
  • @user2773677 `responseText` won't parse it beyond trying to read it as text vs. binary (and assumes the response is UTF-8 encoded). It is the "*raw*" or verbatim form of the response body. So, `\x22` is read as 4 individual characters and is equal to the value of a `"\\x22"` string literal in JavaScript. – Jonathan Lonowski Sep 12 '13 at 18:25
  • Jonathan, i understood. it is mean, that raw value will be must convert to js view ? But any way in raw value we have one slash \x0A what mean new line, and js must escape it,no? Or maybe it happen in JS core, i mean C++ (v8 or smth else) ? – Ralo Sep 12 '13 at 18:35
1

Why not just let the JSON parser do its job and handle the \x escape sequences, and then just convert the string back to hex again afterwards, e.g.

function charToHex(c) {
    var hex = c.charCodeAt(0).toString(16);
    return (hex.length === 2) ? hex : '0' + hex;
}

"Hello".replace(/./g, charToHex);  // gives "48656c6c6f"
Alnitak
  • 334,560
  • 70
  • 407
  • 495