我是角度和离子的新手,所以我开始使用AngularJS创建简单的离子应用程序,但是我遇到了一个问题,我尝试在google上搜索它,但找不到。
用于测试我在角度控制器中的硬编码参数。在ionic应用程序上单击“提交”按钮后,它在API控制器中执行了相关操作,但数据显示为空,
如果有人可以帮忙弄清楚这一点,将不胜感激,
AngularJs控制器
$scope.savenewdansal= function(newDansal){
$http({
method : 'POST',
url : 'http://localhost:51079/api/Dansal/SaveNewDansal',
dataType: 'json',
data: JSON.stringify({"MainCategory": "category1","SubCategory": "subCateMy","District": "Central","City": "Colo","date": "2000-01-01T00:00:00","Time": "2001-01-01T00:00:00","Venue": "kurudnwa","contacts": "nocon"}),
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(data) {
if (data.errors) {
console.log(data.errors.name)
} else {
console.log(data.message);
}
});
WebAPI动作
[HttpPost]
[ActionName("SaveNewDansal")]
public VM_NewDansal SaveDansal(VM_NewDansal newDansal)
{
return newDansal;
}
型号类别
public class VM_NewDansal
{
public string MainCategory { get; set; }
public string SubCategory { get; set; }
public string District { get; set; }
public string City { get; set; }
public string date { get; set; }
public string Time { get; set; }
public string Venue { get; set; }
public string contacts { get; set; }
}
在本地主机上运行的Ionic App和WebAPI Service谢谢
在搜索完Google的另一篇文章后,我为我的问题找到了解决方案,感谢所有尝试帮助我的人。
首先我改变
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
至
headers : {'Content-Type': 'application/json'}
然后它给了我以下错误,
XMLHttpRequest cannot load http://localhost:51079/... Response for preflight has invalid HTTP status code 405
因此找出原因是因为我的Ionic应用程序和Webserice都在本地主机同一机器上但在不同端口中运行,因此需要启用跨域错误。
因此,如果有人要启用CORS,请点击以下链接
否则,请在Internet Explorer中检查它,比较来源时它不会考虑端口。
谢谢
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句