Solved

JSON error on ajax

Posted on 2014-04-07
3
362 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 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 109

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
parse url to form? 7 25
help restore my wordpress site made on WAMP 8 29
Wordpress Contact Form 7 using up whole page 3 18
php error 26 28
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like theā€¦
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

830 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