I am making a ajax Rest call within jquery and it is supposed to return me a list of beans, which I want to show in the jsp. I am able to make the Rest call but I can't get how to get the result in the ajax call and show it in page.
jsp page with the ajax call
<script type="text/javascript">
$(document).ready(function(){
$('#abcForm').submit(function(){
var v = $('#abc').val();
$.ajax({
type: "GET",
dataType: "jsonp",
url: "http://localhost:8080/RestTest/rest/hello/"+v,
});
});
});
</script>
java code
#Path("hello")
public class RestController {
#GET
#Path("{param}")
public Response getMsg(#PathParam("param") String msg) {
String output = "Hello : " + msg;
return Response.status(200).entity(output).build();
}
}
So actually there will be a rest call from the java class and it will return a list which needs to be shown in the jsp page as ajax call. This is where I am stuck.
Help needed.
Thanks
You have a success callback that will be called when the request finishes as successful and will receive the response as the first parameter:
$.ajax({
type: "GET",
dataType: "jsonp",
url: "http://localhost:8080/RestTest/rest/hello/"+v,
success: function(data) {
//do something with data.
}
});
Related
function submitForm() {
var id = $("#id").val();
var name = $("#name").val();
$.ajax({
type : "POST",
contentType: "application/json",
data : JSON.stringify({
"id":id,
"name":name,
}),
dataType: "json",
url : "rest/emp/create",
success : function(data) {
//alert("created Employee"+data.name+data.id);
}
});
}
i want to redirect to another jsp page with json response from spring controller.
in the above java script function i am calling a spring controller to create an employee data. after successful creation i need to redirect to some other jsp page with the created return value from the spring controller which is json value.
Finally i found the answer to this question. But please confirm whether it is right.
HttpHeaders headers = new HttpHeaders();
headers.setLocation(ucBuilder.path("/user/{id}").buildAndExpand(emp.getId()).toUri());
Hello I have been looking at other threads like mine and I can't seem to get my code working!
I need to send the JS array containing ints to a servlet this is current code:
Javascript code:
function sendReminderEmails(){
$("#sendReminderEmails").click(function(){
var people = null;
var peopleBatch1 = null;
$.ajax({
type: "POST",
url:"getAllUnregisteredPeople",
async: false,
success: function(data){
people =JSON.parse(data);
}
});
peopleBatch1 = people.splice(0,200);
$.post("sendReminderEmails", {people:peopleBatch1, mode : "insert"} , function(data){
});
});
}
Servlet Code:
protected void doPost(HttpServletRequest request, HttpServletResponse response){
String peopleIDs[] = request.getParameterValues("people");
}
It keeps returning null! Can someone tell me if I am doing something wrong ?
You must use JSON.stringify to send your JavaScript object as JSON string.
Change your code
var obj = { people: peopleBatch1, mode: "insert" };
$.post("sendReminderEmails",JSON.stringify(obj) , function(data) {});
On Servlet side you need you use
String jsonStr = request.getParameter("people");
then Convert this jsonStr to JSON object.
in "$ajax" you need to pass the required parameter, eg
var myArray={'john', 'paul'};
$.ajax ({
type: "POST",
url:"getAllUnregisteredPeople",
data: {people: myArray}
async: false,
success: function(data) {
people = JSON.parse(data);
}
});
Hello I 'm trying to make a rest call from jquery into a jersey 1.18 server using jsonp
#GET
#Path("jsonp")
#Produces("application/x-javascript")
public JSONWithPadding testJSONP(#QueryParam("callback") String callback){
String retVal="YES";
return new JSONWithPadding( new GenericEntity<String>("{\"test\":\"" + retVal + "\"}"){},callback);
}
and the jquery:
$.ajax({
url: 'http://localhost:9280/manager/jsonp',
type: 'GET',
dataType: 'jsonp',
jsonp: 'callback',
success: function(data){
console.log("success");
alert(data.test);
}
});
ALthough, it seams that JSONWithPadding is not functioning properly, firefix's console returns:
SyntaxError: missing ; before statement jsonp:1
the content of the jsonp is:
{
"callbackName":"jQuery111109478052598614781_1410424986004",
"jsonSource":{
"rawType":"java.lang.String",
"type":"java.lang.String",
"entity":"{\"test\":\"YES\"}"}
}
Any help :) ?
So I have a JSP file where I simply output the json code, and I'm trying to use that json in my javascript script (in another page), but it doesn't seem to work, the console says that that the returned value is undefined. If I give some "real" API URL, it works though.
This is my json_company.jsp
<%#page import="algo.user36.Service"%>
<%# page contentType="application/json" pageEncoding="ISO-8859-1"%>
<%
Service service = new Service();
out.print(service.generatedJsonCompany());
%>
This is my javascript function
function getData() {
$.getJSON( "json_company.jsp", function( data ) {
console.log(data);
});
}
If I go directly to json_company.jsp, it is the excepted output.
Is this a way that isn't supposed to work?
Accordingly to the code that you have provided.
callback([ {"product_description":"Blueberries 1kg","QTE":"54797.0","REVENUS":"357873.6"}, {"product_description":"Mixed Fruits 500g","QTE":"153549.0","REVENUS":"690961.5"}, {"product_description":"Nuts 500g","QTE":"184481.0","REVENUS":"872008.44"}, {"product_description":"Original 1kg","QTE":"51175.0","REVENUS":"300284.4"}, {"product_description":"Raisins 1kg","QTE":"145205.0","REVENUS":"670625.85"}, {"product_description":"Strawberries 500g","QTE":"68685.0","REVENUS":"363077.0"}]);
You return callback function, not json itself that is typical for jsonp request. Try to predefine callback function in your javascript before call.
function callback(data){
console.log(data);
}
Give it a try:
$.ajax({
type: 'GET',
url: "json_company.jsp",
async: false,
jsonpCallback: 'callback',
contentType: "application/json",
dataType: 'jsonp',
success: function(json) {
console.dir(json);
},
error: function(e) {
console.log(e.message);
}
});
And be sure to check your request in firebug\chrome console to exclude 404 code.
I am trying to collect all the form data and send it as a XML to Controller. This XML will further be sent to back end which will take care of it.
There is no need to marshal this XML into an Object.After receiving this XML I just need to send a String success message back.
It is half working. I am able to receive XML message from UI page and able to print it on console. But when I just send success message back UI ajax call receives No conversion from text to application/xml
#RequestMapping(value="/save",method=RequestMethod.POST,consumes={"application/json", "application/xml", "text/xml","text/plain"})
#ResponseBody public String handleSave(#RequestBody String formData)
{
System.out.println("comes here");
System.out.println(formData);
return "Success";
}
$('form').submit(function () {
$.ajax({
url: $(this).attr('action'),
type: 'POST',
processData: false,
data: collectFormData1(),
headers: {
"Content-Type":"application/xml"
},
dataType: 'application/xml',
success: function (data) {
alert('Success:'+data)
},
error: function (jqXHR, textStatus, errorThrown) {
console.log('jqXHR:'+jqXHR+'\n'+'textStatus:'+'\n'+textStatus+'errorThrown::'+errorThrown);
}
});
return false;
});
Try to remove dataType: 'application/xml' from jquery code.
As mentioned in documentation: DataType: The type of data that you're expecting back from the server.
(http://api.jquery.com/jQuery.ajax/)