Solved

JSON error on ajax

Posted on 2014-04-07
3
371 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 22

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 110

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 22

Expert Comment

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The purpose of this video is to demonstrate how to manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…
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…

634 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