Angularjs 文件上传说此请求没有可用的响应数据
Angularjs files uploads says this request has no response data available
我使用下面的代码将图片和用户名上传到服务器。它工作正常。
现在我想return控制台中的用户名和文件名,但它显示此请求没有可用的响应数据。我曾尝试使用 successcallback()
函数,但仍然没有成功。看来问题出在 successcallback()
。谁能帮我解决这个问题。
file.upload = Upload.upload({
method: 'post',
url: 'image.php',
data: {username: $scope.username, file: file},
}).then(function successCallback(response) {
alert(response.data[0].username);
alert(response.data[0].file);
console.log(response.data[0].username);
});
下面是完整代码
//inject angular file upload directives and services.
var app = angular.module('fileUpload', ['ngFileUpload']);
app.controller('MyCtrl', ['$scope', 'Upload', '$timeout','$http', function ($scope, Upload, $timeout, $http) {
$scope.uploadPic = function(file) {
file.upload = Upload.upload({
url: 'upload.php',
data: {username: $scope.username, file: file},
});
file.upload.then(function (response) {
$timeout(function () {
file.result = response.data;
console.log(response.data[0].username);
});
}, function (response) {
if (response.status > 0)
$scope.errorMsg = response.status + ': ' + response.data;
}, function (evt) {
// Math.min is to fix IE which reports 200% sometimes
file.progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
});
}
}]);
image.php
<?php
error_reporting(0);
$data = json_decode(file_get_contents("php://input"));
$username = strip_tags($data->username);
$return_arr[] = array("username"=>$username);
echo json_encode($return_arr);
exit();
上面post中提到的问题已经解决。
我将表单参数用户名发送为 json_decode()
$data = json_decode(file_get_contents("php://input"));
$username = strip_tags($data->username);
将所有内容作为 post 参数发送解决了我的问题,如下例
$username = $_POST['username'];
谢谢
我使用下面的代码将图片和用户名上传到服务器。它工作正常。
现在我想return控制台中的用户名和文件名,但它显示此请求没有可用的响应数据。我曾尝试使用 successcallback()
函数,但仍然没有成功。看来问题出在 successcallback()
。谁能帮我解决这个问题。
file.upload = Upload.upload({
method: 'post',
url: 'image.php',
data: {username: $scope.username, file: file},
}).then(function successCallback(response) {
alert(response.data[0].username);
alert(response.data[0].file);
console.log(response.data[0].username);
});
下面是完整代码
//inject angular file upload directives and services.
var app = angular.module('fileUpload', ['ngFileUpload']);
app.controller('MyCtrl', ['$scope', 'Upload', '$timeout','$http', function ($scope, Upload, $timeout, $http) {
$scope.uploadPic = function(file) {
file.upload = Upload.upload({
url: 'upload.php',
data: {username: $scope.username, file: file},
});
file.upload.then(function (response) {
$timeout(function () {
file.result = response.data;
console.log(response.data[0].username);
});
}, function (response) {
if (response.status > 0)
$scope.errorMsg = response.status + ': ' + response.data;
}, function (evt) {
// Math.min is to fix IE which reports 200% sometimes
file.progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
});
}
}]);
image.php
<?php
error_reporting(0);
$data = json_decode(file_get_contents("php://input"));
$username = strip_tags($data->username);
$return_arr[] = array("username"=>$username);
echo json_encode($return_arr);
exit();
上面post中提到的问题已经解决。
我将表单参数用户名发送为 json_decode()
$data = json_decode(file_get_contents("php://input"));
$username = strip_tags($data->username);
将所有内容作为 post 参数发送解决了我的问题,如下例
$username = $_POST['username'];
谢谢