我發現,這篇文章解決了這個問題對我來說,我使用兩個不同的項目在Visual Studio中一個用於後端的WebAPI,一個用於客戶端。對於這個與角度工作,你需要啓用CORS http://www.codeproject.com/Articles/742532/Using-Web-API-Individual-User-Account-plus-CORS-En#enablecors
此代碼應放入你app.js文件夾
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
// Override $http service's default transformRequest
$httpProvider.defaults.transformRequest = [function (data) {
/**
* The workhorse; converts an object to x-www-form-urlencoded serialization.
* @param {Object} obj
* @return {String}
*/
var param = function (obj) {
var query = '';
var name, value, fullSubName, subName, subValue, innerObj, i;
for (name in obj) {
value = obj[name];
if (value instanceof Array) {
for (i = 0; i < value.length; ++i) {
subValue = value[i];
fullSubName = name + '[' + i + ']';
innerObj = {};
innerObj[fullSubName] = subValue;
query += param(innerObj) + '&';
}
}
else if (value instanceof Object) {
for (subName in value) {
subValue = value[subName];
fullSubName = name + '[' + subName + ']';
innerObj = {};
innerObj[fullSubName] = subValue;
query += param(innerObj) + '&';
}
}
else if (value !== undefined && value !== null) {
query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
}
}
return query.length ? query.substr(0, query.length - 1) : query;
};
return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
}];
,這應該被放入你WebApiConfig.cs文件
//Web API CORS
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
您是否通過POSTMAN測試了API? –
@邵豪林不,我以前從來沒有用過。 – GabeCoach
您可以使用POSTMAN測試您的RESTful API。這是一個鉻工具,你可以下載它並測試你的API。 –