2

I'm trying to get a string response from my controller but I get the below error:

SyntaxError: Unexpected end of JSON input(…) "Error 200"

When I change the response to a boolean or a different type, it's working ok. The problem is when I try to return a string.

js code:

 $.ajax({
   method: "POST",
   url: "./signup",
   data: _data,
   dataType: "json",
   contentType : "application/json;charset=UTF-8",
   success : function(data) {
       console.log(data)
   },
   error : function(qXHR, textStatus, errorThrown){
       console.log(errorThrown, "Error " + qXHR.status);
   }
  });

controller code:

@RequestMapping(value = "/signup", method = RequestMethod.POST, produces = {"text/plain", "application/*"})
    public @ResponseBody String signup(@RequestBody UserSignup details) {
       //...
        return message;
    }

any idea how can I solve this problem? I have tried a few things but nothing work. I think the response format is wrong as what the code expects.

Edit

I have changed my code(removed produces) but I still getting the same error:

SyntaxError: Unexpected end of JSON input(…) "Error 200"

@RequestMapping(value = "/signup", method = RequestMethod.POST)
    public @ResponseBody String signup(@RequestBody UserSignup details) {
        message = "ok";
        }
        return message;
    }

3 Answers 3

5

Your method is wrong. You are saying to produce produces = {"text/plain", "application/*"} But you are also adding the @ResponseBody which will generate JSON format response.

I would suggest you remove the attribute produces. And verify the string you are returning is well formed

Sign up to request clarification or add additional context in comments.

5 Comments

I have tried removing the produces and I'm trying to return an string like "ok" and I still getting the same error. @cralfaro
@gon250 could you please update your question with the data you are testing right now? and the error message? just to be updated at same point as you
I have added the new code. basically I have removed the produces and I make sure I return a string.
remove contentType : "application/json;charset=UTF-8", from your ajax call, and the url is right?? you have a dot url: "./signup",
I have tried all the changes you told me and nothing work(I was getting the same error). I did a different stuff to get it working as I added in an answer. @cralfaro stackoverflow.com/a/40607383/2545964
0

Try to wrap your response in ResponseEntity class

@RequestMapping(value = "/signup", method = RequestMethod.POST)
    public @ResponseBody ResponseEntity<String> signup(@RequestBody UserSignup details) {
        message = "ok";
        return new ResponseEntity<>(message, HttpStatus.OK);
    }

Also double check data that you are sending to server, maybe this is the problem, can you show us _data value?

Comments

0

As I don't have problem when the response is different stuff as a String I have solved the problem creating my own object. So below is the code:

public class Response<T> {
    private T message;
    private Exception ex;

    public Exception getEx() {
        return ex;
    }

    public void setEx(Exception ex) {
        this.ex = ex;
    }

    public T getMessage() {
        return message;
    }

    public void setMessage(T message) {
        this.message = message;
    }
}


@Controller
public class MyControllerController {
    private Response<String> _response;
    private String message;

    public MyController() { _response = new Response<>(); }

 @RequestMapping(value = "/signup", method = RequestMethod.POST)
    public @ResponseBody Response<String> signup(@RequestBody UserSignup details) {
        try{
            message = "";
             // code...
            _response.setMessage(message);
            return _response;
        }catch (Exception ex){
            _response.setEx(ex);
            return _response;
        }
    }
}

response example in the browser:

Object {message: "", ex: null}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.