Solved

JSON error on ajax

Posted on 2014-04-07
3
354 Views
Last Modified: 2014-04-07
hi all,

I'm trying to send some form information to a php file through wordpress using the following code;

var jsonObject = {};
    				jsonObject['name'] = jQuery('#nameInput').val();
				jsonObject['email'] = jQuery('#emailInput').val();
				jsonObject['message'] = jQuery('#messageInput').val();
				jsonObject['product'] = jQuery('#productOwnedInput').val();
				jsonObject['company'] = jQuery('#companyInput').val();
				jsonObject['reason'] = jQuery('#reasonInput').val();
				jsonObject['contact'] = jQuery('#contactInput').val();

				jQuery.ajax({
        				type: "POST",
       					url: "<?php bloginfo('template_url'); ?>/sendform.php",
        				data: JSON.stringify(jsonObject),
        				contentType: "application/json; charset=utf-8",
        				dataType: "json",
					success: function (data) {
       						alert('success');
    					},
					error: function (xhr, ajaxOptions, thrownError) {
        					alert(xhr.status);
        					alert(thrownError);
      					}
    				});

Open in new window


now for some reason it is alerting a 200 error with JSON.Parse: unexpected charatcer.

any ideas why its doing this?
0
Comment
Question by:flynny
  • 2
3 Comments
 
LVL 21

Expert Comment

by:Kim Walker
ID: 39983360
Could any of the form field values contain quote marks? Perhaps you need to urlencode the values.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39983364
Without seeing the data we are just guessing, but it may be related to a character set encoding error.  JSON is strict about its requirement for UTF-8 data.

Suggest you use alert() to print out the JSON string and copy/paste it here.  You may also want to read this article.  It's not directly about JavaScript and JSON, but it may give a clue to how UTF-8 collisions can make a mess of otherwise good work.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11880-Unicode-PHP-and-Character-Collisions.html
0
 
LVL 21

Expert Comment

by:Kim Walker
ID: 39983379
Sorry, wrong language. You need to encodeURI the values.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now