Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 380
  • Last Modified:

JSON error on ajax

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
flynny
Asked:
flynny
  • 2
1 Solution
 
Kim WalkerWeb Programmer/TechnicianCommented:
Could any of the form field values contain quote marks? Perhaps you need to urlencode the values.
0
 
Ray PaseurCommented:
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
 
Kim WalkerWeb Programmer/TechnicianCommented:
Sorry, wrong language. You need to encodeURI the values.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now