jquery - 如何使用JQuery post方法

标签 jquery

$.post('image.php', {
    image:form.image.value  
}


<form id="form" enctype="multipart/form-data">
<input type="file" id="image" name="image"/>

PHP-> isset($_FILES['file'])

如何使用$.post()发布 $_FILES 表单标签内?
我还需要包含enctype 或者我是否需要使用 AJAX,我该怎么做?

最佳答案

使用 jQuery 表单插件来 AJAX 提交带有文件的表单。 http://malsup.com/jquery/form/

在 JS 中

$('#form').ajaxSubmit({
 success: function(response) {
  console.log(response);
 } 
});

在 PHP 中

// after doing upload work etc

header('Content-type: text/json');
echo json_encode(['message' => 'Some message or param whatever']);
die();

完整文档和示例:http://malsup.com/jquery/form/#ajaxSubmit

关于jquery - 如何使用JQuery post方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18127300/

相关文章:

javascript - JS - 处理初始服务器响应以显示 JSON

jQuery 单击前 x 个按钮

jquery - 使用 CSS 调整可调整大小图像的图像描述栏

javascript - 如何使用按钮刷新浏览器页面?

javascript - flowplayer:coverImage 作为数据属性

javascript - jQuery .load() html 页面,里面有 javascript

javascript - 如何在最大宽度 768px 上禁用 jquery 功能?

javascript - 如何在 jquery ajax 中发送所有搜索过滤器值并获得结果?

javascript - 向 JavaScript 函数添加变量不起作用

javascript - 完整日历 : How to support one tap and no long tap