-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error with JSON data #126
Comments
It seems to only be escaping the value ($scope.newResource) however the key (resource:) is parsed correctly along with the file information. $scope.newResouce looks something like this:
|
It applies the angular's default transformRequest which will call Generally it is not a good design to send the data along with the file because the formData elements don't have a content-type and the data needs to be converted to plain text and back on the server side. |
Interesting, I see now that it applies angular's default So, I'm confused as to why |
Could you provide the actual content of the http request from the Chrome network tab. |
Thanks for looking into this. Here is the content of the http request from the network tab when using the file upload directive. Let me know if you were asking for something else:
Here is the content of the network tab when uploading just the form content using
In the first one I have a 500 server error because the params are coming in with escaped quotes and the second one has a 422 unprocessable entity because I purposefully didn't send the file information which is required normally however I can see that the params aren't escaped in the second one. |
You can see that your "resource" form data on the first one is in the correct json format so it might be something on your server side. |
So it has to do with the content type? Because both are the same object and both are being sent through angular's transformRequest but one is received by the server with escaped quotes and the other is not. |
They will both convert it to json string but since the content type is specified in post request the debugger and server side can show and receive it as a proper json object. |
Well, I'm not really sure why this issue is closed. I'm having the same issue:
but it's arriving in node express as:
Based upon what I have read above, I guess I don't understand how to solve this issue. I need access to the JSON on the node side of things. |
Alright, I figured out the problem. When I originally set this up, I was trying to post from angular as:
This was causing a problem because sub-elements of data were getting wrapped in singlequotes. My solution was to do the following:
Now, I can access it from express as:
|
@wmbutler When using your method I still can't see my data in Express. Here's my Angular
And here's my Express
Here's what I see in my Chrome network panel:
I'm using body-parser and connect-busboy and can't get this data. |
I'd suggest a little housekeeping. It's the only way I could keep things straight. I hope this works for you. First, assign the contents of your JSON to a variable:
Then, I had to JSON.Parse on the express side
Let me know how it goes. I wrestled quite a bit with this. |
I found this thread while I was struggling with the same issue. However, I found that my problem was that I was misusing the concept of form data. For those still struggling, this is what I did: Say I have some data that I want to post: var data = { 'stuff': { 'foo': 'bar' } } My initial (wrong) thought was to just use this variable as the data in my upload function: $upload.upload({
url: "/upload",
method: 'POST',
data: data,
file: file
}) That's not going to work and ends up having the same problem people on this thread are talking about, where on the server I receive a string for the value:
We instead have to use the formDataAppender option to format the form data correctly: $upload.upload({
url: "/upload",
method: 'POST',
data: data,
file: file,
formDataAppender: function(formData, key, value){
// key => 'stuff'
// value => { 'foo': 'bar' }
for (k in value) {
var dataKey = key + "[" + k + "]"; // => "stuff[foo]"
formData.append(dataKey, value[k]); // => sets "stuff[foo]=bar" - the correct way to send the form data
}
}
}) I know this is a closed issue, but hopefully that helps anyone who is having the same issue and finds this thread. |
Your problem is in. import library jquery todo parsing $.param(data); is solution your problems. |
In the upload function I am assigning a nested object to data. My code is as follows:
However, the parameters that are being sent to the server isn't being parsed correctly. For some reason the JSON data is being escaped. Shown below:
Any help is much appreciated. Thanks!
The text was updated successfully, but these errors were encountered: