从sharepoint访问MVC web api [英] Access MVC web api from sharepoint
问题描述
当我尝试使用JavaScript获取错误从SharePoint访问MVC web api时。
我尝试了什么:
我按照以下步骤操作 -
1.使用以下步骤创建MVC web api VS并在IIS中托管它,它给出了以下输出(我不知道为什么xml输出在我复制时显示的那样) -
< string xmlns = http://schemas.microsoft.com/2003/10/Serialization/\">
< newdataset> <位置> < id> 1< name> Resturant1< latitude> 18.160859< longitude> 83.924561< desciption> Resturant1测试数据< Image> blue.png< / Image> <位置> < id> 2< name> Resturant2< latitude> 22.018817< longitude> 88.428955< desciption> Resturant2测试数据< Image> blue.png< / Image> <位置> < id> 3< name> Resturant3< latitude> 21.324558< longitude> 86.824951< desciption> Resturant3测试数据< Image> blue.png< / Image> <位置> < id> 4< name> Hotel Taj< latitude> 20.318228< longitude> 86.31958< desciption> Hotel Taj Test Data< Image> green.png< / Image> <位置> < id> 5< name> Hotel Hill< latitude> 25.576511<经度> 88.253174< desciption> Hotel Hill测试数据< Image> green.png< / Image>
2.使用以下JavaScript创建SharePoint Visual Web部件并访问托管的Web api
函数GetLocations(){
$ .support.cors = true;
$ .ajax({
url: 'http://10.11.1.81:82/api/map/getlocations',
类型:POST,
dataType:xml,
crossDomain:true,
dataType:json,
contentType:application / json,
成功:函数( res){
console.log(JSON.stringify(res));
},
错误:function(res){
console.log(坏事发生!+ res.statusText);
}
});
}
3.更改web.config并添加以下标签并保存。
< customheaders>
< add name =Access-Control-Allow-Originvalue =*>
< add name =Access-Control-Allow-Headers value =Content-Type,Accept,X-Requested-With,X-File-Name>
< add name =Access-Control-Allow-Methodsvalue =GET, POST>
4.然后部署WebPart并将其添加到页面中。
5.之后它会给出以下错误 -
SEC7115 :: visit和:link样式可以只是颜色不同。某些样式未应用于:已访问。
index.aspx
SEC7118:XMLHttpRequest for http://10.11.1.81:82/api/map/getlocations需要跨源资源共享(CORS)。
index.aspx
SEC7119:XMLHttpRequest for http://10.11.1.81:82/api/map/getlocations需要CORS预检。
index.aspx
SEC7120:在Access-Control-Allow-Origin标头中找不到原点http:// dc2:5000。
index.aspx
SCRIPT7002:XMLHttpRequest:网络错误0x80070005,访问被拒绝。
index.aspx
坏事发生了!错误
Google Maps API警告:NoApiKeys https://developers.google.com/maps/documentation/javascript/error-messages#no-api-keys
Google地图API警告:SensorNotRequired https://developers.google.com/maps/documentation/javascript/error-messages#sensor-not-required
请帮我解决问题。这很紧急。谢谢提前。
问候,
Arnab
Hi when I am trying to access MVC web api from SharePoint using JavaScript getting error.
What I have tried:
I followed below steps –
1. Create a MVC web api using VS and hosted it in IIS, which gives below output (I don't why xml output show like that when I copy here) -
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
<newdataset> <locations> <id>1 <name>Resturant1 <latitude>18.160859 <longitude>83.924561 <desciption>Resturant1 Test data <Image>blue.png</Image> <locations> <id>2 <name>Resturant2 <latitude>22.018817 <longitude>88.428955 <desciption>Resturant2 Test Data <Image>blue.png</Image> <locations> <id>3 <name>Resturant3 <latitude>21.324558 <longitude>86.824951 <desciption>Resturant3 Test Data <Image>blue.png</Image> <locations> <id>4 <name>Hotel Taj <latitude>20.318228 <longitude>86.31958 <desciption>Hotel Taj Test Data <Image>green.png</Image> <locations> <id>5 <name>Hotel Hill <latitude>25.576511 <longitude>88.253174 <desciption>Hotel Hill Test Data <Image>green.png</Image>
2. Create a SharePoint Visual Web Part and access hosted web api using below JavaScript
function GetLocations() {
$.support.cors = true;
$.ajax({
url: 'http://10.11.1.81:82/api/map/getlocations',
type: "POST",
dataType: "xml",
crossDomain: true,
dataType: "json",
contentType: "application/json",
success: function (res) {
console.log(JSON.stringify(res));
},
error: function (res) {
console.log("Bad thing happend! " + res.statusText);
}
});
}
3. Change in web.config and add below tags and save it.
<customheaders>
<add name="Access-Control-Allow-Origin" value="*">
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept, X- Requested-With, X-File-Name">
<add name="Access-Control-Allow-Methods" value="GET, POST">
4. Then Deploy the WebPart and add it in a page.
5. After that it will give below errors –
SEC7115: :visited and :link styles can only differ by color. Some styles were not applied to :visited.
index.aspx
SEC7118: XMLHttpRequest for http://10.11.1.81:82/api/map/getlocations required Cross Origin Resource Sharing (CORS).
index.aspx
SEC7119: XMLHttpRequest for http://10.11.1.81:82/api/map/getlocations required CORS preflight.
index.aspx
SEC7120: Origin http://dc2:5000 not found in Access-Control-Allow-Origin header.
index.aspx
SCRIPT7002: XMLHttpRequest: Network Error 0x80070005, Access is denied.
index.aspx
Bad thing happend! error
Google Maps API warning: NoApiKeys https://developers.google.com/maps/documentation/javascript/error-messages#no-api-keys
Google Maps API warning: SensorNotRequired https://developers.google.com/maps/documentation/javascript/error-messages#sensor-not-required
Please help me how to resolve the problem. Its urgent. Thanks Advance.
Regards,
Arnab
推荐答案
.support。 cors = true;
.support.cors = true;
.ajax({
url:'http://10.11.1.81:82/api/map/getlocations' ,
类型:POST,
dataType:xml,
crossDomain:true,
dataType :json,
contentType:application / json,
成功:函数(res){
console.log(JSON。 stringify(res));
},
错误:函数(res){
console.log(坏事发生!+ res.statusText);
}
});
}
3.更改web.config并添加以下标签并保存。
< customheaders>
< add name =Access-Control-Allow-Originva lue =*>
< add name =Access-Control-Allow-Headersvalue =Content-Type,Accept,X-Requested-With,X-File-Name >
< add name =Access-Control-Allow-Methodsvalue =GET,POST>
4.然后部署WebPart并将其添加到页面中。
5.之后它会给出以下错误 -
SEC7115 ::访问和:链接样式只能因颜色而异。某些样式未应用于:已访问。
index.aspx
SEC7118:XMLHttpRequest for http://10.11.1.81:82/api/map/getlocations需要跨源资源共享(CORS)。
index.aspx
SEC7119:XMLHttpRequest for http://10.11.1.81:82/api/map/getlocations需要CORS预检。
index.aspx
SEC7120:在Access-Control-Allow-Origin标头中找不到原点http:// dc2:5000。
index.aspx
SCRIPT7002:XMLHttpRequest:网络错误0x80070005,访问被拒绝。
index.aspx
坏事发生了!错误
Google Maps API警告:NoApiKeys https://developers.google.com/maps/documentation/javascript/error-messages#no-api-keys
Google地图API警告:SensorNotRequired https://developers.google.com/maps/documentation/javascript/error-messages#sensor-not-required
请帮我解决问题。这很紧急。谢谢提前。
问候,
Arnab
.ajax({
url: 'http://10.11.1.81:82/api/map/getlocations',
type: "POST",
dataType: "xml",
crossDomain: true,
dataType: "json",
contentType: "application/json",
success: function (res) {
console.log(JSON.stringify(res));
},
error: function (res) {
console.log("Bad thing happend! " + res.statusText);
}
});
}
3. Change in web.config and add below tags and save it.
<customheaders>
<add name="Access-Control-Allow-Origin" value="*">
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept, X- Requested-With, X-File-Name">
<add name="Access-Control-Allow-Methods" value="GET, POST">
4. Then Deploy the WebPart and add it in a page.
5. After that it will give below errors –
SEC7115: :visited and :link styles can only differ by color. Some styles were not applied to :visited.
index.aspx
SEC7118: XMLHttpRequest for http://10.11.1.81:82/api/map/getlocations required Cross Origin Resource Sharing (CORS).
index.aspx
SEC7119: XMLHttpRequest for http://10.11.1.81:82/api/map/getlocations required CORS preflight.
index.aspx
SEC7120: Origin http://dc2:5000 not found in Access-Control-Allow-Origin header.
index.aspx
SCRIPT7002: XMLHttpRequest: Network Error 0x80070005, Access is denied.
index.aspx
Bad thing happend! error
Google Maps API warning: NoApiKeys https://developers.google.com/maps/documentation/javascript/error-messages#no-api-keys
Google Maps API warning: SensorNotRequired https://developers.google.com/maps/documentation/javascript/error-messages#sensor-not-required
Please help me how to resolve the problem. Its urgent. Thanks Advance.
Regards,
Arnab
在ASP.NET Web API 2中启用跨源请求ASP.NET站点
这篇关于从sharepoint访问MVC web api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!