如何设置.asmx webservice使用Asp.net启用跨域 [英] How to set .asmx webservice Enabling Cross-Origin using Asp.net
问题描述
我的网络服务代码是
[WebMethod]
public List<test> GetMachineData_List(string prefix, int person_id)
{
using (var db = new TestDB())
{
List<test> list = db.Fetch<test>("select id,name from machine_data_collection mc where mc.id=@0 and name like '%" + prefix + "%'", person_id);
return list.ToList();
}
}
我的jquery Ajax调用是
My jquery Ajax call is
$("#textbx").autocomplete(
{
source: function (request, response) {
$.ajax({
url: 'http://localhost:4787/ws/webservice.asmx/GetMachineData_List',
data: { prefix: request.term.toString() ,person_id:1},
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
var jsonArray;
try {
jsonArray = $.parseJSON(data.d); // using jQuery
} catch (e) {
jsonArray = data.d; // using jQuery
}
response($.map(jsonArray, function (item) {
return {
id: item.id,
value: item.Name
};
}));
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
var msg = XMLHttpRequest.responseJSON.d;
if (msg == undefined) {
alert( "Something went wrong !!" + errorThrown);
} else {
alert( "Error"+ msg);
}
}
});
},
minLength: 2,
select: function (event, ui) {
var idControl = this.dataset.bindcontrol;
try {
alert(ui.item.id);
}
catch (ex) {
alert( "Oops .. Something happend unexpected !! .. Please redo ");
}
}
}
);
我在web.config代码中启用了Cross-Origin
And i enable the Cross-Origin in web.config code is
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost:21702/" />
<add name="Access-Control-Allow-Headers" value="X-AspNet-Version,X-Powered-By,Date,Server,Accept,Accept-Encoding,Accept-Language,Cache-Control,Connection,Content-Length,Content-Type,Host,Origin,Pragma,Referer,User-Agent" />
<add name="Access-Control-Allow-Methods" value="GET, PUT, POST, DELETE, OPTIONS" />
<add name="Access-Control-Max-Age" value="1000" />
<add name="Access-Control-Allow-Credentials" value="true" />
</customHeaders>
</httpProtocol>
</system.webServer>
当发生文本框中的更改文本错误时。
来自Ajax调用的错误Meaasge是:
When change text in textbox error occured. Error Meaasge from Ajax call is :
XMLHttpRequest无法加载
http:// localhost:4787 / ws / webservice.asmx / GetMachineData_List 。响应
预检请求未通过访问控制检查:否
'Access-Control-Allow-Origin'标头出现在请求的
资源上。因此,不允许
访问来源' http:// localhost:21702 '。响应的HTTP状态代码为500.
XMLHttpRequest cannot load http://localhost:4787/ws/webservice.asmx/GetMachineData_List. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:21702' is therefore not allowed access. The response had HTTP status code 500.
推荐答案
在<$ c $中添加以下代码段c> Web.config 文件:
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
.............
..............
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Headers" value="accept, content-type" />
<add name="Access-Control-Allow-Origin" value="http://localhost:4200"/>
<add name="Access-Control-Allow-Methods" value="POST, GET, OPTIONS" />
**...(add additional add names and values when you need).......**
</customHeaders>
</httpProtocol>
</system.webServer>
在我的情况下< add name =Access-Control-Allow- Originvalue =http:// localhost:4200/>
webservice消费者应用程序(Angular)的基本URL是 http:// localhost:4200
。
In my case <add name="Access-Control-Allow-Origin" value="http://localhost:4200"/>
the base url of webservice consumer app (Angular) is http://localhost:4200
.
您必须在该特定位置提及您的基本网址。
You have to mention your base url in that specific place.
这篇关于如何设置.asmx webservice使用Asp.net启用跨域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!