Solved

JSON error on ajax

Posted on 2014-04-07
3
359 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 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 22

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
phpmyadmin 3 33
WordPress syntax 2 28
Ajax Panel Not Updating When Triggered By Timer 4 44
Make an ajax call ithat alters url and updates a listview in asp.net 1 22
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 …
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 Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…

867 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

16 Experts available now in Live!

Get 1:1 Help Now