Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery AJAX post is empty when content-type is set to JSON UTF-8
    text
    copied!<p>I'm trying to post some very simple data to a php file using jquery and then get the json response but I seem to be running into a road block somewhere. Here is my jquery:</p> <pre><code>&lt;script&gt; $(function() { $('.resend-verify').click( function() { var userid = $(this).attr('rel'); $.ajax({ type: "POST", url: "resend_verification.php", contentType: "application/json; charset=utf-8", data: "userid=" + userid, dataType: "json", success: function(data) { console.log(data.response); if(data.response == 'error') { $('div.alert').addClass('error'); } $('div.alert').html(data.comment); } }); }); }); &lt;/script&gt; </code></pre> <p>and here is the php page it posts to</p> <pre><code>&lt;?php if ($_SERVER['REQUEST_METHOD'] == "POST") { // Check For Post require '../../config.php'; // Site Settings require '../../connectors/mysqlConnector.php'; include $DIR['php'] . 'functions.php'; // Common Functions //var_dump(json_decode($_POST)); $UserID = $_POST['userid']; $userSQL = "SELECT p.user_firstname,p.user_lastname,a.user_email,a.user_salt FROM web_profiles p INNER JOIN web_accounts a ON p.user_id = a.user_id WHERE p.user_id ='" . $UserID . "'"; $userQuery = mysql_query($userSQL); //var_dump($userSQL); $user = mysql_fetch_object($userQuery); if (!$user-&gt;user_email) { $response = array('response' =&gt; 'error', 'comment' =&gt; 'User not found'); } else { // Send User Verification Email $sendmail = new sendMail(); $message = createUserAuthEmail($user-&gt;user_firstname, $user-&gt;user_lastname, $user-&gt;user_salt, $Site['register_email_body']); $content['body'] = '&lt;br /&gt;&lt;br /&gt;' . $message . '&lt;br /&gt;&lt;br /&gt;DO NOT REPLY TO THIS EMAIL! IT IS ONLY AN AUTOMATED NOTIFICATION EMAIL!'; $sendmail-&gt;set(to, $user-&gt;user_email); $sendmail-&gt;set(subject, 'Action Required to Activate Membership'); $sendmail-&gt;set(from, 'no-reply@domain.com'); $sendmail-&gt;set(html, true); $sendmail-&gt;getParams($content); $sendmail-&gt;parseBody(); $sendmail-&gt;setHeaders(); if ($sendmail-&gt;send()) { $response = array('response' =&gt; 'success', 'comment' =&gt; 'email sent'); } else { $response = array('response' =&gt; 'error', 'comment' =&gt; 'Error sending email'); } } echo json_encode($response); } ?&gt; </code></pre> <p>The problem im having is that if I use contentType: "application/json; charset=utf-8" the $_POST is always empty. And when I remove contentType: "application/json; charset=utf-8" the $_POST is populated but I cant get a json response. What am I doing wrong??</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload