Skip to content

Instantly share code, notes, and snippets.

@jabley
Created May 18, 2011 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jabley/979756 to your computer and use it in GitHub Desktop.
Save jabley/979756 to your computer and use it in GitHub Desktop.
var buddy = {};
buddy.firstName=$('input[name="fname"]').val();
buddy.lastName=$('input[name="lname"]').val();
buddy.email=$('input[name="email"]').val();
var data = JSON.stringify(buddy);
$.ajax({
type: 'POST',
url: "/path/to/handler",
data: data,
contentType: 'application/json',
dataType: 'json',
success : function (data) {
// ...
},
error: function (jqXHR, textStatus, err) {
// ...
}
});

Successful input

buddy

{ firstName:"foo", lastName:"bar", email:"foo@gmail.com" };

data

'{ "firstName":"foo", "lastName":"bar", "email":"foo@gmail.com" }';

POST sends

{ "firstName":"foo", "lastName":"bar", "email":"foo@gmail.com" }

Failing input

buddy

{ firstName:"foo", lastName:"bar", email:"??@gmail.com" };

data

'{ "firstName":"foo", "lastName":"bar", "email":"??@gmail.com" }';

POST sends

{ "firstName":"foo","lastName":"bar","email":"jQuery152011057530349834899_1305758039649@gmail.com" }

and the error function is called with an err object:

"jQuery152011057530349834899_1305758039649 was not called"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment