illegal character in jsonp response - ajax

Hi i am doing an exercise where I am trying to scrape content of nytimes website using javascript/ajax.
In order to send cross domain ajax request I am using a proxy server that returns me jsonp response with nytimes website content.
But since that html content has some charaters (single quotes) which throws "Illegal Character" error.
<script type="text/javascript" src="http://json-proxy.jgate.de/?callback=callback&url=http%3A%2F%2Fwww.nytimes.com%2F"></script>
<script>
function callback(obj) {
alert(obj);
}
</script>
What could be a possible solution to this error?
And what are other alternatives to solve this?
Thanks
(p.s. This question was asked to one my friends in his interview)
Update: Here is yahoo pipes link for the same.
http://pipes.yahoo.com/pipes/pipe.run?_id=748e37c218ed0747d0b868ae8eafefa6&_render=json

You have to escape the characters on the server.
callback('<!DOCTYPE html PUBLIC \"-//W...etc.') // notice the \"-//W
Instead of:
callback('<!DOCTYPE html PUBLIC "-//W...etc.') // which will eventually create an error
Escape all the special characters like /, \, ", ' to /, \ ...

Yahoo pipes fixed that issue.

Related

How can I find a specific link in the HTML response Code with the name?

I have a html link structure like that <a href=dynamicURL>LinkName</a>.
I want to extract the LinkName, so I can take the dynamic URL from the link for next HTML Request.
Is there any solution with a script or something like that?
Use a Regular Expression Extractor with the regex:
<a\shref=([^>]+)>LinkName<\/a>
\s = space
[^>]+ = everything not equal to >
\/ = escaping the forward slash
Try this site, it makes learning regular expressions easy.

Outlook handling of quoted url parameters in mailto link

I'm attempting to populate the body of a mailto link with an html link. The target browser is IE 7+ and mail client is Outlook 2007+. Before I ask my question, i'll acknowledge the fact that the body parameter is intended for short text messages as called out here:
https://stackoverflow.com/a/4304779/573083
and detailed here:
The special "body" indicates that the associated is the body of the
message. The "body" field value is intended to contain the content for
the first text/plain body part of the message. The "body" pseudo
header field is primarily intended for the generation of short text
messages for automatic processing (such as "subscribe" messages for
mailing lists), not for general MIME bodies. Except for the encoding
of characters based on UTF-8 and percent-encoding, no additional
encoding (such as e.g., base64 or quoted-printable; see [RFC2045]) is
used for the "body" field value. As a consequence, header fields
related to message encoding (e.g., Content-Transfer-Encoding) in a
'mailto' URI are irrelevant and MUST be ignored. The "body" pseudo
header field name has been registered with IANA for this special
purpose (see Section 8.2).
That being said, there have been a number of threads on SO with varying levels of success with inserting links in the body tag. for example: https://stackoverflow.com/a/1455881/573083 and https://stackoverflow.com/a/9138245/573083
My issue is similiar, but it is specifically with outlook rendering quoted parameters of embedded links. I currently have the following that is almost working:
A link
A partial link appears correctly in the outlook body, however outlook is not including the final quoted url parameter ("somevalue") in the link; the ="somevalue" is just appearing as plain text. Viewing the source of the email message shows that outlook is closing the enclosing <a> tag as it is interpreting the %22 as the end of the link. I've attempted to escape the %22 with %2f, /, ' - to no avail. I believe that I need the correct sequence for outlook to understand that the %22 should be included in the link, and not as the closure of the enclosing link.
Any help would be appreciated.
Judging by the ?, you haven't encoded the body component.
> encodeURIComponent("http://someserver.somedomain/somepage.aspx?id=1234%26somekey=%22somevalue%22")
"http%3A%2F%2Fsomeserver.somedomain%2Fsomepage.aspx%3Fid%3D1234%2526somekey%3D%2522somevalue%2522"
So the code should be:
A link
Or more likely:
A link
I would put the link inside "<" & ">".
%20 = space
%0D = new line
%3C = "<"
%3E = ">"
<html>
<body>hi
A link</body>
</html>

Why Firefox wont transfer %20 to space (' ')?

I am sending to the browser a request to save a file with the file name.
The file name might include spaces, so i replace all spaces with %20.
Internet Explorer and Chrome transfers %20 back to spaces, but Firefox does not to that. why?
Is there a way make all browsers show the space?
This is my code:
String codedName = new String(URLEncoder.encode(name, "UTF-8"));
codedName = codedName.replaceAll("\\+", "%20");
response.setHeader("Content-Disposition", "attachment; filename=\"" + codedName+ "\"");
That depends on how you create the file name. Usually, you can simply set the file name in the header field and the framework will encode it properly. In your case, you seem to encode the name twice. Try without encoding it.
You may use Javascript to encode the url.
The Syntax for encoding URL's in JavaScript is:
encodeURI(uri)
So, the code would be: (Note the space in-between my and test.)
<script type="text/javascript">
var uri="my test.html?name=jason&age=25";
document.write(encodeURI(uri)+ "<br />");
</script>
Which results in:
my%20test.html?name=jason&age=25
As per your recent comment "How do I do it in Java?"
The syntax would be something like:
encode(String s)
A simple Google search would reveal more information.

What's the correct way to get HTML from an AJAX Response in JSON Format?

I have an AJAX request that creates a 'post', and upon successful post, I want to get HTML to inject back into the DOM. Right now I'm returning a JSON array that details success/error, and when I have a success I also include the HTML for the post in the response. So, I parse the response as JSON, and set a key in the JSON array to a bunch of HTML Code.
Naturally, the HTML code is making the JSON array break -- what should I do to escape it (or is there a better way to do this?). I get an AJAX response with a JSON array like so:
[{response:"success"},{html:'<div class="this is going to break...
Thanks!
Contrary to what you're probably used to in JavaScript, ' can't begin a string in JSON. It's strictly a ". Single quotes work when you're passing JSON to JavaScript.. much like <br> works when you want to put an XHTML line break.
So, use " to open the HTML string, and sanitize your quotes with \".
json.org has more info WRT what you should sanitize. Though the list of special characters isn't long, it's probably best to use a library like Anurag suggests in a comment.
Apart from escaping double quotes as mention by BranTheMan, newlines also break JSON strings. You need to replace newlines with \n.
Personally I've found this to be enough:
// Don't know what your serverside language is, example in javascript syntax:
print(encodeJSON({
response : "success",
html : htmlString.replace(/\n/g,'\\n').replace(/"/g,'\\"')
}));

Displaying partially Unicode encoded data via AJAX/innerHTML

I am trying to get some data from the server via an AJAX call and then displaying the result using responseDiv.innerHTML. The data from the server comes partially encoded with Unicode elements, like: za\u010Dat test. By setting the innerHTML of the response div, this just displayed as is. That is, the Unicode is not converted to an actual representation in the browser.
The charset of the containing page is set to UTF-8. I have tried most other things, like converting the unicode representation to HTML entities, but that doesn't seem to work either.
I should also mention that the text coming from the server has HTML tags intermixed as well. The HTML tags are honored as they should be. For example, if the text from the server comes as <b>Bold this!</b>, the text is bolded.
Any help appreciated.
Vikram
Can you replace '\u010D' with 'č'?
AFAIK the HTML tags coming from the server should work if you are setting the innerHTML.
This works for me:
document.getElementById('info').innerHTML = "č <b>Bold this</b>";
BTW - you can use something like Fiddler or Firebug to ensure you are getting what you expect from the server.
Update: use regular expressions to find and replace the unicode characters with HTML entities:
$.get('data.txt', function(data) {
data = data.replace(/\\u([0-9A-F])([0-9A-F])([0-9A-F])([0-9A-F])/g, '&#x$1$2$3$4;');
document.getElementById('info').innerHTML = data;
});
Just convert the unicode literals to characters directly:
'H\u0065\u006Clo, world!'.replace(/\u([0-9a-fA-F]{4})/, function() {
return String.fromCharCode(parseInt(arguments[1],16));
});

Resources