这是我的HTML表单:
<form name="myForm" ng-submit="">
<input ng-model='file' type="file"/>
<input type="submit" value='Submit'/>
</form>
Run Code Online (Sandbox Code Playgroud)
我想从本地计算机上传图像,并想要读取上传文件的内容.我想用AngularJS做的所有这些.
当我尝试打印$scope.file它的值时,它是未定义的.
我有这个jQuery代码,可以很好地交叉起源:
jQuery.ajax({
url: "http://example.appspot.com/rest/app",
type: "POST",
data: JSON.stringify({"foo":"bar"}),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (response) {
console.log("success");
},
error: function (response) {
console.log("failed");
}
});
Run Code Online (Sandbox Code Playgroud)
现在我想把它转换成Angular.js代码而没有任何成功:
$http({
url: "http://example.appspot.com/rest/app",
dataType: "json",
method: "POST",
data: JSON.stringify({"foo":"bar"}),
headers: {
"Content-Type": "application/json; charset=utf-8"
}
}).success(function(response){
$scope.response = response;
}).error(function(error){
$scope.error = error;
});
Run Code Online (Sandbox Code Playgroud)
任何帮助赞赏.
我有一个带标签的表格 ng-submit="login()
该函数在javascript中被称为fine.
function LoginForm($scope, $http)
{
$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
$scope.email = "fsdg@sdf.com";
$scope.password = "1234";
$scope.login = function()
{
data = {
'email' : $scope.email,
'password' : $scope.password
};
$http.post('resources/curl.php', data)
.success(function(data, status, headers, config)
{
console.log(status + ' - ' + data);
})
.error(function(data, status, headers, config)
{
console.log('error');
});
}
}
Run Code Online (Sandbox Code Playgroud)
我从PHP文件中得到200 OK响应,但是,返回的数据是这样email并且password未定义.这是我的所有PHP
<?php
$email = $_POST['email'];
$pass = $_POST['password'];
echo $email;
?>
Run Code Online (Sandbox Code Playgroud)
知道为什么我得到未定义的POST值吗?
编辑
我想指出,因为这似乎是一个受欢迎的问题(但它已经过时了),.success并且.error已经被弃用了,你应该使用 …
我有一个服务:
angular.module('cfd')
.service('StudentService', [ '$http',
function ($http) {
// get some data via the $http
var path = 'data/people/students.json';
var students = $http.get(path).then(function (resp) {
return resp.data;
});
//save method create a new student if not already exists
//else update the existing object
this.save = function (student) {
if (student.id == null) {
//if this is new student, add it in students array
$scope.students.push(student);
} else {
//for existing student, find this student using id
//and update it.
for (i …Run Code Online (Sandbox Code Playgroud) 我正在读一本名为"Pro Angular JS"的书.但是,我有一个关于如何捕获错误状态的问题.
我编码的是:
$http.get(dataUrl)
.success(function (data){
$scope.data.products = data;
})
.error(function (error){
$scope.data.error=error;
console.log($scope.data.error.status); // Undefined!
// (This is the spot that I don't get it.)
});
Run Code Online (Sandbox Code Playgroud)
如果我编码"console.log($ scope.data.error.status);" ,为什么console.log的参数未定义?
在书中,有句子,"传递给错误函数的对象定义了状态和消息属性."
所以我做了$ scope.data.error.status
为什么这是错的?
我正在写我的webApp,而且我正在使用AngularJS.在这个应用程序中,我创建了一个名为script.js的文件,并报告此代码:
var modulo = angular.module('progetto', ['ngRoute']);
// configure our routes
modulo.config(function ($routeProvider, $httpProvider) {
$routeProvider
// route for the home page
.when('/', {
templateUrl: 'listaFilm.html',
controller: 'listaController'
})
// route for the description page
.when('/:phoneName', {
templateUrl: 'description.html',
controller: 'descriptionController'
});
$httpProvider.defaults.headers.common['Access-Control-Allow-Origin'] = '*';
});
modulo.controller('listaController', function ($scope, $http) {
$http.get('https://api.getevents.co/event?&lat=41.904196&lng=12.465974').success(function (data) {
$scope.names = data;
}).
error(function (data, status) {
$scope.names = "Request failed";
});
});
Run Code Online (Sandbox Code Playgroud)
使用此代码,我按照RESTful原则调用API.当我运行代码时,我遇到了这个问题:
XMLHttpRequest无法加载https://api.getevents.co请求的资源上没有"Access-Control-Allow-Origin"标头.原产地" 的http://本地主机:8383 "因此不允许访问.
在网上阅读我明白我有一个叫CORS的问题...我已经尝试了几个解决方案,但我没有解决问题.
我该如何解决这个问题?
我必须添加什么代码来修复它?
得到此错误:
angular.min.js:122 TypeError:$ http.get(...).成功不是getUserInfo(app.js:7)中的函数,在new(app.js:12)处于Object.invoke(angular.min) .js:43)在Q.instance(angular.min.js:93)at p(angular.min.js:68)at g(angular.min.js:60)at g(angular.min.js:61) )在angular(min.min.js:61)处于angular.min.js:60 at angular.min.js:21
这是我的代码:
var gitHub = angular.module('gitHub', []);
gitHub.controller('mainController', ['$scope', '$http', function($scope, $http) {
var $scope.user = '';
function getUserInfo($scope, $http){
$http.get('https://api.github.com/users')
.success(function (result) {
$scope.user = result;
console.log(result);
});
};
getUserInfo($scope, $http);
}]);
Run Code Online (Sandbox Code Playgroud)
这是html
<!DOCTYPE html>
<html ng-app="gitHub">
<head>
<title>Github Users Directory</title>
<script src="angular.min.js"></script>
<script src="app.js"></script>
</head>
<body>
<div ng-controller="mainController">
<div>
<h1>GitHub Users</h1>
Who do you want to search for?<input type="text" name="FindHim" ng-model="queryName" />
<button ng-click="getUserInfo()">Search</button>
</div>
<div>
{{ user …Run Code Online (Sandbox Code Playgroud) 我刚开始学习Angular.js.如何在Angular.js中重写以下代码?
var postData = "<RequestInfo> "
+ "<Event>GetPersons</Event> "
+ "</RequestInfo>";
var req = new XMLHttpRequest();
req.onreadystatechange = function () {
if (req.readyState == 4 || req.readyState == "complete") {
if (req.status == 200) {
console.log(req.responseText);
}
}
};
try {
req.open('POST', 'http://samedomain.com/GetPersons', false);
req.send(postData);
}
catch (e) {
console.log(e);
}
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止所拥有的 -
function TestController($scope) {
$scope.persons = $http({
url: 'http://samedomain.com/GetPersons',
method: "POST",
data: postData,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function (data, status, headers, config) {
$scope.data = data; // how do …Run Code Online (Sandbox Code Playgroud) 我发现很难理解"延迟反模式".我认为我原则上理解它,但我没有看到一个超级简单的例子,一个服务,一个不同的承诺和一个反模式,所以我想我会尝试自己做但看到我不是超级了解它我先得到一些澄清.
我在工厂(SomeFactory)中有以下内容:
//url = 'data.json';
return {
getData: function(){
var deferred = $q.defer();
$http.get(destinationFactory.url)
.then(function (response) {
if (typeof response.data === 'object') {
deferred.resolve(response.data);
} else {
return deferred.reject(response.data);
}
})
.catch(function (error) {
deferred.reject(error);
});
return deferred.promise;
}
Run Code Online (Sandbox Code Playgroud)
我检查它的对象的原因只是在上面添加一个简单的验证层 $http.get()
以下,在我的指令中:
this.var = SomeFactory.getData()
.then(function(response) {
//some variable = response;
})
.catch(function(response) {
//Do error handling here
});
Run Code Online (Sandbox Code Playgroud)
现在我的理解是,这是一个反模式.因为原始的延期承诺会捕获错误并简单地吞下它.它不会返回错误,因此当调用此"getData"方法时,我已经执行了另一个捕获错误的捕获.
如果这不是反模式,那么有人可以解释为什么两者都要求各种"回调"吗?当我第一次开始写这个工厂/指令时,我预计不得不在某个地方做一个默认的承诺,但我没有预料到必须.catch()在两边(也就是我有点想我可以让工厂返回响应或错误,如果我做了一个SomeFactory.getData()
angularjs angularjs-directive angularjs-service angular-promise
我目前正在使用angular的$ q服务来进行API调用,如下所示:
var deferred = $q.defer();
$http.get(config.apiHost + details.url)
.success(function (data) {
deferred.resolve(data);
}).error(function (msg) {
deferred.reject(msg);
});
return deferred.promise;
Run Code Online (Sandbox Code Playgroud)
但是我们也可以在不使用$ q的情况下使用这种方法:
return $http.get(config.apiHost + details.url)
.success(function (data) {
return data;
}).error(function (msg) {
return msg;
});
Run Code Online (Sandbox Code Playgroud)
并且当$ http本身返回承诺时,我也可以使用更简化的方法:
$http.get(config.apiHost + 'posts')
.success(function (data) {
console.log(data)
}).error(function (msg) {
console.log(msg);
});
Run Code Online (Sandbox Code Playgroud)
那么所有这些特别是在$ q和$ http之间有什么区别,因为两者都返回promise并且都是异步的?angular是否为$ q提供了一些额外的功能?我找不到任何好的答案.
angularjs ×10
javascript ×4
angular-http ×2
ajax ×1
angular-ui ×1
cors ×1
cross-domain ×1
html ×1
jquery ×1
php ×1