gpt4 book ai didi

javascript - Ajax 响应未显示在页面上

转载 作者:行者123 更新时间:2023-11-28 17:06:07 25 4
gpt4 key购买 nike

我的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,但页面上仍然没有显示任何内容。

最佳答案

PHP 端:

您需要打印相同的变量来表示成功和失败:

if($result) {
$msg['error'] = 'true';
$msg['msg'] = $msg_true;
} else {
$msg['error'] = 'false';
$msg['msg'] = $msg_error;
}

JavaScript 端:

AJAX 响应将如下所示

data.error -> true or false.
data.msg -> Success or Error message depending upon program logic.

...
success: function(data){
$('#success_message').fadeIn().html(data.msg);
...

关于javascript - Ajax 响应未显示在页面上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55811276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com