I have a stringified JSON-Object inside another JSON-Object. I get it via $.ajax() from my database like this:
data => {"foo":"[{\"foo\":\"bar\"},{\"foo2\":\"bar2\"}]"}
When trying to do JSON.parse(data.foo), I get an Uncaught SyntaxError: Unexpected token \.
What am I doing wrong? (Apart from the fact, that the object needs to be saved as a string.)
You have to escape html tags. Which language are you using in back end? In python Django it is done by using the safe filter. Similar might exist in php or other languages.
Thank you for your help. It seems, the data coming from the database "looked" right, but wasn't. I assume this was because I put the data in the database by hand.
Now I put the data via my API with JSON.stringify() and now everythings looks fine.
Related
So, I am using Zapier.com for my High school project.
in which inside the zapier it has a Node js server which provides a async fucntion to runs some codes.
I want to shorten urls using javascript code in the code by zapier pathway so that any links provided will get shortened.
Inputs have no problem .
I get the links and can produce them in the output.
but when i use js with bitly api unable to get an output .
Errors encountered are : [object OBject] OR undefined.
Hello Mikey!
[object Object] is the default value that you get when trying to stringify an object.
To debug this I recommend console logging the object, this will show you all the properties that it contains!
undefined means you are trying to access something that is not defined (hence the name),
To fix this, once again: try to log the entire object and then navigating yourself from there!
Hope this helped :)
I have C# razor *.cshtml template file and would like to use it for Angular (5) server side rendering.
C# .cshtml snippet:
#helper RenderLookupComponent()
{
<list-lookup
[data]="getLookupData('#Model.LookupSourceType')"
[lookupType]="'#Model.LookupSourceType'">
</list-lookup>
}
Everything works fine until '#Model.LookupSourceType' value is not contains escape symbol ('\'), for e.g. when #Model.LookupSourceType = 'SomeType\'
Angular template parser Error:
[error]: Template parse errors:
Parser Error: Missing expected ) at column # in [getLookupData('SomeType\')]
At first glance, it looks like ordinary issue, actually it can be solve by replacing all '\' to '\'. (#Model.LookupSourceType.Replace(#"\", #"\\")).
Q: But i believe there should be more elegant, universal approaches to solve such kind of problems.
The approach that can handles all possible scenario for different kind of escape symbols
Please share your experience
Problem was solved with using HttpUtility.JavaScriptStringEncode(Model.LookupSourceType)
I have an opencart shop. Locally, uploading images work without a glitch. Online, it works only if the image is very small. If I upload an image of, say 300kb, the loading gif next to upload keeps spinning and I get the errors:
ajaxupload.js:
Line 609: if (response) {
Line 610: response = eval("(" + response + ")");
Line 611: } else {
Line 612: response = {};
Line 613: }
Why is this happening?
EDIT:
I did console.log(response) and you were right, what came back was the HTML of the 404 page. But how can it be too big? It works if the image is 100kb but doesn't if it's 130kb.
Why did I get downvoted?
Your Post request is 404ing so the response is not parseable JSON. Looks like your URL has unencoded /s in the query variables. Make sure to use encodeURIComponent() or some other function to make sure your url is properly escaped
From the error I believe that your response is not a valid JSON.
By the way eval is evil so it's much better to use JSON.parse
You can check your JSON here: http://jsonlint.com/
Edit: somebody asked why eval is evil so here are some reasons
it's slower than JSON.parse since it actually starts a compiler
if not done correctly you can end up with XSS attacks
it inherits the execution context and the scope in which its invoked
If you need to support old IE versions you also don't need to use eval(). You could use the excellent JSON library: https://github.com/douglascrockford/JSON-js
This will offer you... JSON.parse support for old IEs.
Try to rewrite the line just before that error.. (don't copy and paste it).. Works sometimes
Does anyone know why the ajax statement here won't accept the '-' symbol. I've had trouble with this in the past and had to change a lot of things in the backend of my site to send the "_" instead and it worked. Is there a way to make the ajax script accept the '-' sympbol?
$("#events").append("<img src='"+data[i].event-image+"'></a>");
This code is part of an ajax call to get some JSON data that my website is sending out. "event-image" is the name of my field. The consol throws an error saying can't find "image". So the dash is making the code ignore the first part "event-". Any thoughts on how I could rectify this, other than changing the field on my site to be "_". Let me know, thanks!
- is used for subtraction. You can do it like this though:
data[i]["event-image"]
variable.property is equivalent to variable["property"] in javascript.
Just try using data[i]["event-image"].
I'm using prototype to make Ajax requests. Occasionally the responses contain malformed XML. Prototype is using the onException callback, but the first error that is thrown is when I am trying to access a particular node.
I know Firefox recognizes the malformed response, because if I request the xml via the address bar, Firefox presents the error.
Is there a way for me to catch a "malformed xml" error in javascript?
With javascript typically you're relying on the browser to parse the XML for you. If the browser can't parse it due to it being malformed, you're going to have to tackle it manually. There appears to be a library for doing that at http://xmljs.sourceforge.net/ . I haven't used it myself, but it appears solid. Then again, it might also throw malformed xml errors at you.
What's causing the malformed xml? Maybe there's something you can do on that end?
And finally, if you're just trying to access some part of the document's data, you could consider using a regular expression:
doc = "<one><two>three</two></one>";
captures = doc.match(/<two>(.*)<\/two>/); // returns ["<two>three</two>", "three"]
data = captures[1]; // "three"