Ajax响应未显示在页面上

我的ajax是


    $.ajax({

        type: 'POST',

        url: ajax.ajax,

        contentType: false,

        processData: false,

        dataType: 'JSON',

        status: 200,

        data: formdata,

        success: function(msg){

            $('#success_message').fadeIn().html(data);

            setTimeout(function() {

                $('#success_message').fadeOut("slow");

        }, 2000 );


        }

    });

这是PHP的一部分


function form(){ 


    global $wpdb;

    $table = cars;


    foreach ($_FILES as $file) {

        if($file['error'] == UPLOAD_ERR_NO_FILE) {

            continue;

        }


        $valid_ext = array( 'img' , 'png');

        $extension_upload = strtolower(  substr(  strrchr($file['name'], '.')  ,1)  );

        if ( in_array($extension_upload,$valid_ext) ) {

            $name_upload = uniqid() . $file['name'];

            $url_insert = trailingslashit( plugin_dir_path( dirname( __FILE__ ) ) ) . 'uploads';

            wp_mkdir_p($url_insert);

            $name_insert = trailingslashit($url_insert) . $name_upload;

            $action = move_uploaded_file($file['tmp_name'],$name_insert);


            $data = array( 'customer_resume' => $name_upload );

            $format = array( '%s' );


            $success=$wpdb->insert( $table, $data, $format );  

            $msg_true = 'Upload ok ';


        } else {

            $msg_error = 'Upload error';

        }

    }


    $result = !isset($msg_error);

    $msg = array();


    if($result) {

        $msg['error'] = 'true';

        $msg['true'] = $msg_true;

    } else {

        $msg['error'] = 'false';

        $msg['false'] = $msg_error;

    }


    header('Content-Type: application/json');

    echo json_encode($msg);


}

以及我尝试显示成功或错误消息的HTML


<div id="error_message"></div>

<div id="success_message"></div>

当我单击“提交”按钮时,我一切正常,并保存在数据库中,但是没有任何迹象表明是否成功。我尝试添加此msg,但页面上仍然没有任何显示。


翻翻过去那场雪
浏览 177回答 2
2回答

冉冉说

PHP方面:您需要打印相同的变量以获取成功和失败:if($result) {&nbsp; &nbsp; &nbsp; &nbsp; $msg['error'] = 'true';&nbsp; &nbsp; &nbsp; &nbsp; $msg['msg'] = $msg_true;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $msg['error'] = 'false';&nbsp; &nbsp; &nbsp; &nbsp; $msg['msg'] = $msg_error;&nbsp; &nbsp; }JavaScript方面:AJAX回应会如下data.error -> true or false.data.msg -> Success or Error message depending upon program logic....&nbsp;success: function(data){&nbsp; $('#success_message').fadeIn().html(data.msg);...

沧海一幻觉

“ ajax.ajax”背后隐藏着什么?另外,如果要显示数据,则需要使用“ msg”success: function(msg){&nbsp; &nbsp; &nbsp; &nbsp; $('#success_message').fadeIn().html(msg);&nbsp; &nbsp; &nbsp; &nbsp; setTimeout(function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#success_message').fadeOut("slow");&nbsp; &nbsp; }, 2000 );&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP