I am using angular 2 as front end frame work and node js in the back end, So I am trying to send image with a json object that contains some particular information about that image. So I am wondering , if it is safe to pass that json object in the URL.
No, it's not safe, as your JSON object might be very long, too long for a URL. You will need to send an AJAX request where your JSON will be a parameter. Example of asynchronous request sending a JSON object:
xhttp.open("POST", "ajax_test.asp", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("data=" + JSON.stringify(yourobject));
then, on the server you will have a POST parameter called data where you will have everything you need.
Related
A rather basic Angular 8 question I assume, but here goes:
I am using http.get() to pull a JSON response from the server. If I save the response as a variable, e.g. jsonResponse, and put that response into my HTML component as {{jsonResponse | json}} I get the anticipated nice json object, e.g. { 'hithere': {'this is me' : 'no really'}}.
But I cannot seem to get the same nice Object to loop through in the TS/JS side of things. Can someone point me in the correct direction so that I can do something like. jsonResponse.hithere?
Try passing the object using JSON.parse(obj) and then loop/traverse through its leafs.
I'm building a REST API in JAVA and C# and I was wondering about the way I should pass data to those services.
What I'm familiar with as the right way is to send JSON object as the data in the POST body:
{name:'Dor'}
but I can also pass a string and parse the JSON in my service:
'{name:'Dor'}'
What is the preferable way from performance factor? or any other factors?
Basically, if you need to send the json data across via jquery, then we need to use stringify, else the data would be serialized into to key=value pair.
So, you cannot send the json object directly via jquery ajax method.
How it works behind the hood:
In $.ajax function, if we provide data as
data :{key1:"value1", key2:"value2"}
is serialized to key1=value1&key2=value2
if we provide data as
data :'{key1:"value1", key2:"value2"}' or JSON.stringify({key1:"value1", key2:"value2"})
is sent as {key1:"value1", key2:"value2"}
So, what we can conclude is that, we cannot pass json object directly via jquery, we can send only json string. Hope this clarifies everyone.
i'm developping a program in java ee.
i know how to display a view from a servlet with a code like this
this.getServletContext().getRequestDispatcher( VUE).forward( request, response );
where my view (VUE) is in a jsp page.
i know also how to return a json object with something like that
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(jsonErreurEtRole);
I'm using google Gson to convert java objects.
Now i want to return a json object that contain a view in one field and some others messages in others field.
But i can't find how to do it.
Is there a way for that ?
EDIT
I will try to be more clear,
My problem is that : users ara requesting content by calling a servlet (a controller), i check their right on that content. And if they don't have right i send a message like "you don't have right ..."
that message should not replace any content in the html page but just appear as a notification.
So i wanted to return a json object like [message, content]
by the way i solved my problem by sending only message in a json object if user don't have right and html content if he has. And in je jquery code i'm testing the return type of the response.
I used this post :
jQuery ajax returned data: json and html mix?
I have JSON string that I get from AS3, and I need to send part of it, for example:
{"player": "player", "x_accel": "10", "y_accel": "10"}
And I want to send only x_accel, because that was the only thing that changed.
Use a Backbone Model. From Backbone Model.save doc:
"If instead, you'd only like the changed attributes to be sent to the server, call model.save(attrs, {patch: true}). You'll get an HTTP PATCH request to the server with just the passed-in attributes."
I currently have a servlet returning a JSON string on a POST to the output stream of the response.
This is my code:
...
response.setContentType("application/json");
PrintWriter out = response.getWriter();
out.print(jsonString);
out.flush();
I'm not sure how to handle this on the client side as it just displays the string on the page. What needs to be done?
A servlet returning a JSON is not meant to be called directly by the browser. It's meant to be called with JavaScript or another artifact that can interpret JSON.
Usually you will have something like:
var myObject = JSON.parse(myJSONtext, reviver);
That will get you an object parsed from JSON contents you send from servlet.
To get myJSONtext you usually do an AJAX call within a piece of Java Script code.
Google for: json ajax example
You will get a lot of information online.