JQuery AJAX message is empty when content type is set to JSON UTF-8

I am trying to publish some very simple data in a php file using jquery and then get a json response, but it looks like I am running into a road block somewhere. Here is my jquery:

<script>
    $(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);
                }

            });

        });       
    });
</script>

and here is the php page to which he sends messages

<?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->user_email) {
        $response = array('response' => 'error', 'comment' => 'User not found');
    } else {
        // Send User Verification Email
        $sendmail = new sendMail();
        $message = createUserAuthEmail($user->user_firstname, $user->user_lastname, $user->user_salt, $Site['register_email_body']);

        $content['body'] = '<br /><br />' . $message . '<br /><br />DO NOT REPLY TO THIS EMAIL! IT IS ONLY AN AUTOMATED NOTIFICATION EMAIL!';
        $sendmail->set(to, $user->user_email);
        $sendmail->set(subject, 'Action Required to Activate Membership');
        $sendmail->set(from, 'no-reply@domain.com');
        $sendmail->set(html, true);
        $sendmail->getParams($content);
        $sendmail->parseBody();
        $sendmail->setHeaders();
        if ($sendmail->send()) {
            $response = array('response' => 'success', 'comment' => 'email sent');
        } else {
            $response = array('response' => 'error', 'comment' => 'Error sending email');
        }
    }
    echo json_encode($response);
}
?>

Im problem is that if I use contentType: "application / json; charset = utf-8", $ _POST is always empty. And when I delete contentType: "application / json; charset = utf-8", $ _POST is populated, but I can not get the json response. What am I doing wrong?

+3
source share
3 answers

jquery documentation, , data dataType. data key/value, :

data: { 'userid': userid }

... dataType xml, html, text, json jsonp. PHP Content-type (, header('Content-type: text/json');), ( " " ). JQuery . , , , , HTML HTML-, jQuery. , PHP script, UTF-8.

:

  • PHP, , $_POST ,
  • PHP script, - curl wget, ,
  • javascript- script, , ; ,
  • javascript, script (, Firebug Firefox , Chrom [e | ium]); , , ,
+1

, , PHP:

$_ POST: , script HTTP POST , , application/x-www-form-urlencoded multipart/form-data HTTP Content-Type .

jquery, xmlhttprequest. , , $_POST , JSON , % encoded, , , , JQuery, , , JSON, "application/x-www-form-urlencoded" JSON urlencoded . PHP $_POST , "", JSON . , $_POST - - , , , , $HTTP_RAW_POST_DATA, , "php://input" - :

$rawPOST = file_get_contents('php://input');

, JSON , .

, - , 4 , , 7000 .

+1

You should try to use $.postand associate it with the event, if necessary. I did this for the mousemove event:

$.fn.saveArray = function() { 
    var data = {data: selectednumbers};
    $.post('clickmap.php',{
        data:JSON.stringify(data)
        //contentType: 'application/json',
    });
};
0
source

Source: https://habr.com/ru/post/1792628/


All Articles