ajax success function is printing [object Object] instead of plain text. Why? - java

jQuery code:
function ajaxsubmit(){
$.ajax({
url: "/update",
type: "POST",
dataType: "html"
}).success(function(data) {
$('#result').html(data);
});
}
and my Java function:
public static Result ajaxupdate() {
String done = "very good";
return ok("very good").as("text/plain");
}
the alert is giving [object Object], instead of plain text "very good". why?

you want to use:
alert(JSON.stringify(data));
so you JavaScript will look like:
function ajaxsubmit(){
$.ajax({
url: "/update",
type: "POST",
}).complete(function(data) {
alert(JSON.stringify(data));
});
}
Your Java code looks like it is wrapping your string into an object before it sends it back to the client, JSON.stringify() will show you the structure of the object that is being returned and from there you can work out what property of the returned object contains your return variable (Probably something like data.data or data.return)

add dataType: "text" and change complete() with success()
function ajaxsubmit(){
$.ajax({
url: "/update",
type: "POST",
dataType: "html"
}).success(function(data) {
$('#result').html(data);
});
}

The jQuery documentation clearly answers your question. From http://api.jquery.com/jQuery.ajax/
complete(jqXHR, textStatus)
<...>
two arguments: The jqXHR (in jQuery 1.4.x, XMLHTTPRequest) object and a string
You can find more about jqXHR in documentation.
If you want to use the response string, consider opting for .success method. You may have to explicitly provide .contentType

Related

Add compiler to java pages

i would like to add a compiler to my jsp pages where the user can enter his code and compile it. Any idea whats the method to add a compiler to jsp pages?
Heh. I tried to put in where I got this approach from, but the spam filter prevented it. I can't vouch for whether the urls below are open for anyone to use, but this is one way to do it:
function submitForm(){
jQuery.support.cors = true;
$('#wait').show();
if ($.browser.webkit || $.browser.mozilla) {
var url = "http://www.compileonline.com/compile_new.php";
}else{
var url = "col_proxy.php";
}
$.ajax({
type: "POST",
cache: false,
crossDomain: true,
url: url,
target: "view",
data: $("#ff").serialize(),
success:function(data)
{
$('#view').contents().find("html").html(data);
$('#wait').hide();
return false;
},
error:function (data, status, error) {
alert(error);
return false;
}
});
return false; // avoid to execute the actual submit of the form.
}

Use ajax success return formatted data to populate easyUI form

I am trying to use backend returned data to populate textboxes in the form. If I replace the "result" var with full raw
data(eg.{
name:'myname',
email:'mymail#gmail.com',
subject:'subject',
message:'message',
language:'en'
}
it works... So it make wonder whether the returned data is not formatted properly or what?
$.ajax({
type: "post",
cache: false,
contentType: 'application/x-www-form-urlencoded; charset=utf-8',
url: "<%=path%>/storeRace",
data:{
type:'query', venue:'<%=race.getVenue()%>', date:'<%=race.getDate().toString()%>', race_num:'<%=race.getRaceNum()%>'
},
success:function(result){
if(result=="none"){
}
else{
alert(result);
$('#raceForm').form('clear');
$('#raceForm').form('load', result);
}
},
error:function(result){
$.messager.alert('Error',result,'error');
}
});

How I write data in span when i am using jQuery EasyUI

I am using AJAX for data communication from server but in case of success i want to write the data into span(id=cartcount) but it's not worked please help how i achieve this concept
function addToCart(pid, name) {
if (confirm("Are you sure to add "+name + " to cart?")) {
$.ajax({
type: 'POST',
url: 'addcart',
data: {
pid: pid
},
success: function(obj) {
$('#cartcount').val(obj);
}
});
}
}
try:
$('#cartcount').html(obj);
or
$('#cartcount').text(obj);

How to parse parameters that sent jquery table

I writing a simple Spring MVC web application that use JQuery DataTable on the client-side. DataTable using server-side processing mode. The DataTable make request with parameters(in my case):
draw:1
columns[0][data]:name
columns[0][name]:
columns[0][searchable]:true
columns[0][orderable]:true
columns[0][search][value]:
columns[0][search][regex]:false
columns[1][data]:type
columns[1][name]:
columns[1][searchable]:true
columns[1][orderable]:true
columns[1][search][value]:
columns[1][search][regex]:false
columns[2][data]:action
columns[2][name]:
columns[2][searchable]:true
columns[2][orderable]:true
columns[2][search][value]:
columns[2][search][regex]:false
order[0][column]:0
order[0][dir]:asc
start:0
length:10
search[value]:
search[regex]:false
I don't know how can i parse parameters like columns[i][data], columns[i][name], columns[i][searchable], etc. The reason is why, because I don't know how many table columns I will have. How to solve this problem?
This is my Spring controller:
#RequestMapping(value = "/getImageWrappers", method = RequestMethod.POST)
public String getImageWrappers(#RequestParam Integer draw,
#RequestParam Integer start,
#RequestParam Integer length,
#RequestParam(value = "search[value]") String searchText){
}
and DataTable configuration:
$('#imageWrapperTable').DataTable({
columns:[
{"data" : "name"},
{"data" : "type"},
{"data" : "action"}
],
"processing": true,
serverSide: true,
ajax: {
url: '/getImageWrappers.json',
type: 'POST'
}
});
I just figure it out by adding this code
$('#imageWrapperTable').DataTable({
columns:[
{"data" : "name"},
{"data" : "type"},
{"data" : "action"}
],
"processing": true,
serverSide: true,
ajax: {
url: '/getImageWrappers.json',
type: 'POST',
datatype: 'json',
data: function(d){
//add your custom param here
d.name = "zxbing";
//this will put all query strings to a json object string
return JSON.stringify(d);
}
}
});
In this way, you just need to transfer only a query string to JSON object on the server side.
In a json array I would do something like this with jquery:
function displayData(x) {
x.success(function(data) {
$.each(data.columns, function(i, paramenter) {
$.each(parameter, function(a, b) {
$('#somediv').append('column'+parameter+' -> data: '+b+' ');
});
});
}),
x.error(function(data) {
//error message
})
}//end displayData
So you can loop through your array, but you gotta play around with it, I haven't tested it but in general that is the idea. Hope it helps.

json string can't be posted as plain string through jquery ajax?

I want to post a json string as plain string to a action ,then convert the string to List using gson, but the string is still treated as json object by jquery/webwork, I'm using jquery 1.43+webwork+gson, no jquery json plugin or something.
here is action:
public class ImageAction extends BaseAction {
private String pks;
public void setPks(String pks) {
this.pks = pks;
Gson gson=new Gson();
List<Map> list=gson.fromJson(pks,new TypeToken<List<Map<String,String>>>(){}.getType());
System.out.println(list.size());
}
......
}
jquery code:
j$.ajax({
url:approveUrl,
data: {pks:'[{"userName":"theoffspring"}]'},
// dataType:'json',
type:'post',
// traditional: true,
success:function (response) {
hideProgressBar(parent.document)
if (response.result==false){
alert(response.msg);
return;
}
// document.location.reload();
}
})
I want pks posted as a common string instead of json object. But setPks method turns out not to be invoked when I invoke the jquery code. So strange.
you have'nt serialized the data you are sending through ajax.serialize it at client using JSON.stringify() and send it will be converted to a single string.
modify your code to:
$.ajax({
url:approveUrl,
data:JSON.stringify(yourdata),
// dataType:'json',
type:'post',
// traditional: true,
success:function (response) {
hideProgressBar(parent.document)
if (response.result==false){
alert(response.msg);
return;
}
// document.location.reload();
}
})
this might work.
Have a look at this: http://jsfiddle.net/flocsy/vuGL9/
You'll see that your pks is actually sent as a string as you want it, when it's not sent as a string (pks2) it'll look different.
PS: look at the network tab in firebug or inspect element, depending on your browser:
pks: '[{"userName":"theoffspring"}]'
pks2[0][userName2]:'hehe'
So probably your server side does some magick...

Categories