The ajax success function prints [object Object] instead of plain text. What for?

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");
}

warning gives [object Object]instead of plain text "very good". What for?

+5
source share
3 answers

add dataType: "text" and change change () with success ()

function ajaxsubmit(){
    $.ajax({
        url: "/update",
        type: "POST",
        dataType: "html"
    }).success(function(data) {
          $('#result').html(data);
      });
    }
+2
source

which you want to use:

alert(JSON.stringify(data));

so you will look like this:

function ajaxsubmit(){
$.ajax({
    url: "/update",
    type: "POST",
}).complete(function(data) {
      alert(JSON.stringify(data));
  });
}

Your Java code looks like this: it wraps your string in an object before it sends it to the client, JSON.stringify () will show you the structure of the returned object and from there you can determine that the property of the returned object contains the returned variable (maybe something like data.data or data.return)

+4

jQuery . 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 the documentation. If you want to use the answer string, consider choosing a .success method. You may need to explicitly provide .contentType

+2
source

All Articles