Rueben Abankwah
Rueben Abankwah

Reputation: 33

Uncaught TypeError: Cannot read property 'message' of undefined

I am trying to implement an AJAX contact form for my site, the problem I am encountering is that when I click on submit I get the following error in Google Chrome:

"Uncaught TypeError: Cannot read property 'message' of undefined----contact-form.js:38".

Please find the HTML code and contact-from.js.

Upvotes: 3

Views: 32527

Answers (2)

Jonathan Lonowski
Jonathan Lonowski

Reputation: 123563

The error is because responseJSON isn't a standard property or one that jQuery makes available for XMLHttpRequests.

You'll want to use responseText instead, which may be parsable:

contactForm.addAjaxMessage($.parseJSON(response.responseText).message, true);

// or
contactForm.addAjaxMessage(response.responseText, true);

Upvotes: 1

Chris
Chris

Reputation: 26908

error: function(response) {
     contactForm.addAjaxMessage(response.responseJSON.message, true);
}

If you take a look at the jQuery documentation for error in $.ajax, you'll see that the third parameter to the callback function is a string containing the error message:

error

Type: Function( jqXHR jqXHR, String textStatus, String errorThrown )

A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred.

So, try this, instead:

error: function(a, b, response) {
     // You can use `a` and `b` if you need them.

     contactForm.addAjaxMessage(response, true);
}

Upvotes: 0

Related Questions