我一直在尝试找出如何寻求帮助,我确信那里有很多信息,但我不知道要搜索什么。我正在使用 Visual Studio 2013 的 apache-cordova 插件。我正在使用 AngularJS 创建一个应用程序。我需要对我的 MVC 5 项目进行 Ajax 调用。我可以在不使用身份验证的情况下进行 GET 调用,但我不能允许匿名 POST 调用。我会在我的 Ajax 中包含自定义标题吗?这是我的第一个移动应用程序,所以我不确定它是如何工作的。这是我如何拨打我的 GET 电话
app.factory('ItemGet', function ($http, $q) {
return {
query: function () {
var deferred = $q.defer();
$http({ method: 'get', url: 'http://example.com/api/apiItem' })
.success(function (data) {
deferred.resolve(data);
}).error(function (error) {
deferred.reject(error);
});
return deferred.promise;
}
}
});
更新
//Post New Job
$scope.submitJob = function () {
var data = {
username: $scope.loginForm.username,
password: $scope.loginForm.password,
JobId: $scope.JobId,
JobNumber: $scope.currentItem.JobNumber,
JobName: $scope.currentItem.JobName,
}
var json = JSON.stringify(data);
$http.post('http://example.com/api/apiJob/PostNewJob', json, {headers: {'Accept': 'application/json', 'Content-Type': 'application/json'}})
.success(function(data, status) {
// success code
})
.error(function(data, status) {
// error code
});
};
错误
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4400' is therefore not allowed access.
我在服务器端启用了 Cors
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.EnableCors();
我做了一些搜索,但我不确定到底该怎么做。
我想我找到了一个临时解决方案?
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost:4400" />
</customHeaders>
</httpProtocol>
我的控制台如何显示来自我的服务器的登录页面。这是什么意思?
<h2>Log in.</h2>
<div class="row">
<div class="col-md-8">
<section id="loginForm">
<form action="/Account/Login?ReturnUrl=%2Fapi%2FapiJob" class="form-horizontal" method="post" role="form"><input name="__RequestVerificationToken" type="hidden" value="akPAxvS8E2QJHtxFuSnHnFzdSE1Rs1HQExTcNIofMwCKIEZ6NZowUfO2SJZhAAPhxbK178KDOug9g0Y0Sn6_obTvM9cHj77b70btqRfHgwg1" /> <h4>Use a local account to log in.</h4>
<hr />
<div class="form-group">
<label class="col-md-2 control-label" for="Email">Email</label>
<div class="col-md-10">
<input class="form-control" data-val="true" data-val-email="The Email field is not a valid e-mail address." data-val-required="The Email field is required." id="Email" name="Email" type="text" value="" />
<span class="field-validation-valid text-danger" data-valmsg-for="Email" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label" for="Password">Password</label>
<div class="col-md-10">
<input class="form-control" data-val="true" data-val-required="The Password field is required." id="Password" name="Password" type="password" />
<span class="field-validation-valid text-danger" data-valmsg-for="Password" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<div class="checkbox">
<input data-val="true" data-val-required="The Remember me? field is required." id="RememberMe" name="RememberMe" type="checkbox" value="true" /><input name="RememberMe" type="hidden" value="false" />
<label for="RememberMe">Remember me?</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Log in" class="btn btn-default" />
</div>
</div>
<p>
<a href="/Account/Register">Register as a new user</a>
</p>
</form> </section>
</div>