当azure web应用程序请求azure web API资源时,我得到的没有‘访问-控制-允许-原产地’标题在请求的资源错误上存在,但是在本地它工作正常,当我访问蔚蓝网站时,我在部署后得到上面的错误。
以下是代码:
角js服务
function industrysearchservice(appConfig, $q) {
var dsIndustry;
var schemaIndustry = {
data: function (response) {
return response.value;
},
total: function (response) {
return response['@odata.count'];
},
model: {
id: "Industry"
}
};
getIndustries = function (filter) {
var deferred = $q.defer();
var dsFetch = new kendo.data.DataSource({
batch: false,
schema: schemaIndustry,
type: "odata-v4",
serverFiltering: true,
serverSorting: true,
serverPaging: true,
pageSize: 20,
transport: {
read: {
url: appConfig.odataUri + "/PSellerIndustryFilter",
dataType: "json",
data: {
$select: "Industry"
}
}
}
});
if (!angular.isUndefined(filter))
dsFetch._filter = filter;
dsFetch.fetch().then(function () {
dsIndustry = dsFetch;
deferred.resolve(dsIndustry);
});
return deferred.promise;
}
return { getIndustries: getIndustries };}
控制器方法:
public class PSellerIndustryFilterController : ODataController
{
PSellerContext db = new PSellerContext();
private bool PSellerIndustryExists(System.Guid key)
{
return db.PSellerIndustryFilters.Any(p => p.Industry == key.ToString());
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
[EnableQuery]
public IQueryable<PSellerIndustryFilter> Get()
{
return db.PSellerIndustryFilters;
}
[EnableQuery]
public SingleResult<PSellerIndustryFilter> Get([FromODataUri] System.Guid key)
{
IQueryable<PSellerIndustryFilter> result = db.PSellerIndustryFilters.Where(p => p.Industry == key.ToString());
return SingleResult.Create(result);
}
}发布于 2016-12-09 16:54:31
我之前遇到过这个问题。要么通过代码或通过azure门户启用CORS (或者通过ARM模板实现自动部署),而不是在两个地方都启用CORS,如果您在这两个地方都这样做--至少这是我的经验。我建议从蔚蓝的门户开始。
导航到资源组下的web应用程序,然后搜索CORS设置-->然后添加*或您希望允许的特定来源。如果这管用的话。
注意:确保通过门户将CORS设置添加到您的Odata站点,而不是您的web应用程序
通过门户 https://learn.microsoft.com/en-us/azure/app-service-api/app-service-api-cors-consume-javascript
通过ARM https://github.com/azure-samples/app-service-api-dotnet-todo-list/blob/master/azuredeploy.json
通过代码 WebApiConfig.cs
config.EnableCors();PSellerIndustryFilterController.cs
[EnableCors(origins: "http://Yoururl.azurewebsites.net", headers: "*", methods: "*")]
public class PSellerIndustryFilterController : ODataController
{
//details
}发布于 2016-12-09 15:47:09
您应该向请求中添加标题,因为有CORS:
transport: {
read: {
url: appConfig.odataUri + "/PSellerIndustryFilter",
dataType: "json",
data: {
$select: "Industry"
}
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
}https://stackoverflow.com/questions/41064035
复制相似问题