使用JQuery在Ajax中读取响应文本
Reading response text in Ajax using JQuery
function upload_file(){
var file =document.getElementById('computer_image').files[0];
if(file!==null){
if(file.type==='image/jpeg' ||
file.type==='image/png' ||file.type==='image/jpg'){
$('#progressbar').show();
var formData = new FormData();
formData.append("file1", file);
$.ajax({
url: 'file_upload/ImageUpload',
type: 'POST',
headers: {"abc": "aaaaaaaaa"},
xhr: function() {
var myXhr = $.ajaxSettings.xhr();
if(myXhr.upload){
myXhr.upload.addEventListener('progress',progressHandler, false);
}
return myXhr;
},
success: function( request,data, textstatus){
alert(textstatus.getResponseHeader('abc'));
},
error:errorHandler,
data: formData,
cache: false,
contentType: false,
processData: false
});
}
else {
alert('sorry we are not accepting file other than PNG , JPEG and JPG');
}
}
}
我使用CodeIgniter框架。下面是我的PHP代码来处理文件。
function ImageUpload(){
$status="";
if (empty($_FILES["file1"]))
{
$status = "sorry Something went wrong";
$this->output->set_status_header('400');
echo "sorry Something went wrong";
}
if ($status !== "sorry Something went wrong")
{
//call a function to get path name
$path="./upload";
$config['upload_path'] = $path;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size' ] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['remove_spaces']=TRUE;
$config['overwrite'] = FALSE;
$this->load->library('upload', $config);
/* If directory doesn't exist than create a new .*/
if (!file_exists($path) && !is_dir($path)) {
mkdir($path);
}
/* If there is any error during upload */
if ( ! $this->upload->do_upload('file1')){
$this->output->set_status_header('400');
echo "sorry Something went wrong";
}
/*Image has been successfully Uploaded */
else{
$var = $this->upload->data('','');
echo $path.'/'.$var["file_name"].'='.$var["image_width"].
'='.$var["image_height"];
}
}
我尝试了多种方式来获得响应文本,但没有成功。完成后应该读什么回应文本。得到null
作为输出。
在$.ajax()
的success
方法中,第一个参数将给出response
的例子:
$.ajax({
success: function(response, textStatus, jqXHR ){
console.log(response);
alert(response.getResponseHeader('abc'));
},
});
查看链接。这将有助于更清楚地理解$.ajax()
。
相关文章:
- 如何从java中的URL读取JavaScript响应
- 未捕获的类型错误:无法读取 null 的属性“removeChild”(使用 Instagram 的 JSONP 响应)
- 在 Angular2 和 ionic 中读取 API 响应
- Angular无法读取http post响应中的cookie
- 正在读取angularjs中的HTTP服务器标头响应消息
- 如何读取在节点中发出的简单http请求的响应
- Json使用selfhost c#输出,并使用javascript读取响应
- Ajax在发送响应时从服务器读取响应
- dojo/request/iframe中读取xml响应的缺陷
- Java脚本将URL的响应读取到变量中
- 读取已解析的 JSON 响应
- 如何使用 ajax 或 jquery 读取异步 HTTP (servlet3.0) 响应
- 从 PHP 读取 json 响应并推送到 javascript 多维数组中
- 从 http 适配器过程读取响应
- 为什么在未读取响应的情况下,节点http客户端仍在运行
- 在Ajax php和mysql中读取响应
- 从JavaScript's fetch中重新读取响应体
- 使用JQuery在Ajax中读取响应文本
- 在ajax请求完成之前,以块的形式读取响应
- 使用 iron-ajax,如何读取响应的标头