0

I have a server that is responding to a GET and send JSON data. That data contains some html also, and the following piece of code gives me a Uncaught SyntaxError: Unexpected token ILLEGAL. Now I have seen the multiple posts stating that it could be related to \200B and I think it is.. I have tried this post and I could not find the character.. Can you let me know how I can find and remove this character? This html is coming from a SQL server and I have a lot of data that will have this same string and want to remove it.

Here is the jfiddle that when you run, you get the error.

If I peek into my mssql database (and when I print in console this.model.toJSON().body), I see the following:

<p>Lorem Ipsum</p>
<p>Lorem Ipsum</p>

<strong>Tweet this article out</strong></span><br><br>

<a href="https://twitter.com/share" class="twitter-share-button" data-text="I 25 by @oberz" data-via="Atdsfm" data-size="large">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?\'http\':\'https\';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+\'://platform.twitter.com/widgets.js\';fjs.parentNode.insertBefore(js,fjs);}}(document, \'script\', \'twitter-wjs\');</script>
<p> More html stuff</p>

Here is what this.model.toJSON() looks like at the console.. (the body key has the html I listed above):

Object {id: "former-obama-religion-adviser-speaks-at-event", authorbio: "sdflkd", organization_slug: null, body: "*html shown above*", author_image_url: "", cost: ""…}
Community
  • 1
  • 1
Trewq
  • 3,187
  • 6
  • 32
  • 50
  • What does the Twitter widget code have to do with your question? – coreyward Mar 12 '16 at 04:24
  • I believe the U200B exisits in that piece of code.. If I remove that code, I do not get the error. I updated the question to make it more clear. – Trewq Mar 12 '16 at 04:32
  • Your code is full of backslashes that don't belong. – hobbs Mar 12 '16 at 04:47
  • So you are receiving back a JSON response that includes that `` as a string value? And those escape characters are surviving the string interpolation? Or are you just copying the relevant snippet from the response from your network panel? – Andrew Mar 12 '16 at 04:51
  • Yes, I am receiving back a JSON response that includes that as a string value. The piece of code is from the network pane after I print it out like this on the console.. this.model.toJSON().body. This snippet looks identical when I examine the db where this snippet resides – Trewq Mar 12 '16 at 05:00

1 Answers1

0

So, based on your comments, you are getting back something like this:

{"script":"<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?\'http\':\'https\';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+\'://platform.twitter.com/widgets.js\';fjs.parentNode.insertBefore(js,fjs);}}(document, \'script\', \'twitter-wjs\');</script>"}

This being the case, those backslashes should disappear when JS unpacks the JSON response and hands you a complete object. You can try placing a breakpoint inside your AJAX callback/handler and inspect the contents of that object and its values.

But if you try to copy-paste this code from the response text stream (such as from the network panel or by loading the request directly from your browser's address bar), it won't work because those escape characters shouldn't be there. They are legal only within a string literal (that is, when surrounded by quotes).

Andrew
  • 14,204
  • 15
  • 60
  • 104
  • my apologies if I misstated anything, but I updated my question to provide more data.. – Trewq Mar 12 '16 at 05:16
  • I think my answer still is what you need. The issue is that you are trying to inject an escaped string manually into a context where the quotes break things, rather than letting JS do its own thing. Remember that escape characters are necessary to carry string values across contexts, so if what you see contains escaped characters like `\n` or `\"`, they are necessary only to move the text from one place to another as a string. Once you use `JSON.parse()` (or leave things up to whatever AJAX library you are using), the object you get back will not have those escaped characters any more. – Andrew Mar 12 '16 at 05:32
  • Your answered lead me to the real problem - the data stored in the sql db was escaped.. perhaps incorrectly – Trewq Mar 12 '16 at 21:17