0

I want to use JavascriptMVC for my new project, but I have a problem.

client = new Client($('#create-client-form').formParams());
client.save(successhandler, errorhandler);

documentation:

You just need to call success back with an object that contains the id of the new instance and any other properties that should be set on the instance.

But what if validation fails on server side? In this case my callback should be an object containing error messages, and not an id? Anybody using JavascriptMVC and handeled this situation?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Tamás Pap
  • 17,777
  • 15
  • 70
  • 102

1 Answers1

1

It is up to you how to handle server validation errors or any server errors for that matter. You can either still return a 200 success HTTP code, and then the successhandler will get called on the javascript side, and you need to handle whatever the server comes back with.

A better way is for the server to return some 4xx HTTP error code (you can use an existing one or pick your own), and send a different json object in the response and then the errorhandler will get called and will get passed in these parameters: (jqXHR, textStatus, errorThrown)

source: jQuery ajax

In the error handler, you can parse the json out of the jqXHR.

Hope that helps!

Saket Patel
  • 6,573
  • 1
  • 27
  • 36
Michael
  • 556
  • 3
  • 12