来自javascript的CORS ASP.NET Web服务
我试图弄清楚如何使一个常规html页面(使用javascript)从我在Visual Studio中使用Asp.Net创建的Web服务中获取数据。我的最后一个项目是将WebService托管在一个域中,结果将需要由另一个域中的html页面接收 我真的很难弄清楚如何设置Web服务,以便我的页面可以与之对话 我一直在努力做到: 这里的视频是: 但我似乎无法解决问题。当我尝试使用Firefox加载页面时,没有返回任何内容。我几乎可以肯定,这个问题与我试图使用的URL有关,但我不知道应该是什么 使用javascript从函数HelloWorld2获取返回值的最佳方法是什么 提前谢谢 网络服务来自javascript的CORS ASP.NET Web服务,javascript,asp.net,web-services,cors,Javascript,Asp.net,Web Services,Cors,我试图弄清楚如何使一个常规html页面(使用javascript)从我在Visual Studio中使用Asp.Net创建的Web服务中获取数据。我的最后一个项目是将WebService托管在一个域中,结果将需要由另一个域中的html页面接收 我真的很难弄清楚如何设置Web服务,以便我的页面可以与之对话 我一直在努力做到: 这里的视频是: 但我似乎无法解决问题。当我尝试使用Firefox加载页面时,没有返回任何内容。我几乎可以肯定,这个问题与我试图使用的URL有关,但我不知道应该是什么 使用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace StoneSoupMockup
{
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://SomeDumbUniqueID.org/", Description="This is a sample service for Stone Soup.", Name="MyService")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public string HelloWorld2(string something)
{
return "Hello " + something;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Services;
名称空间StoneSoupMockup
{
///
///WebService的摘要描述
///
[WebService(命名空间=”http://SomeDumbUniqueID.org/“,Description=“这是石汤的示例服务。”,Name=“MyService”)]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
//要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。
[System.Web.Script.Services.ScriptService]
公共类服务1:System.Web.Services.WebService
{
[网络方法]
公共字符串HelloWorld()
{
返回“你好世界”;
}
[网络方法]
公共字符串HelloWorld2(字符串)
{
返回“Hello”+某物;
}
}
}
我在web服务项目的web.config中将访问控制头添加到httpProtocol中,如下所示
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
</system.webServer>
JavaScript:
//WebServices.js
function myAjax() {
var xmlHttp = new XMLHttpRequest();
//I'm really not sure about this URL. This is what Visual Studio has for the
//URL when I test my app
var url="http://localhost:62033/Service1.asmx/HelloWorld2";
//var parameters = "first=barack&last=obama";
var parameters = "something=timmy"
xmlHttp.open("POST", url, true);
//Black magic paragraph
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", parameters.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
document.getElementById('ajaxDump').innerHTML+=xmlHttp.responseText+"<br />";
}
}
xmlHttp.send(parameters);
}
//WebServices.js
函数myAjax(){
var xmlHttp=new XMLHttpRequest();
//我真的不确定这个URL。这是Visual Studio为
//测试我的应用程序时的URL
变量url=”http://localhost:62033/Service1.asmx/HelloWorld2";
//var parameters=“first=barack&last=obama”;
var parameters=“something=timmy”
open(“POST”,url,true);
//黑魔法段落
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
setRequestHeader(“内容长度”,parameters.length);
setRequestHeader(“连接”,“关闭”);
xmlHttp.onreadystatechange=函数(){
if(xmlHttp.readyState==4&&xmlHttp.status==200){
document.getElementById('ajaxDump')。innerHTML+=xmlHttp.responseText+“
”;
}
}
send(参数);
}
HTML代码:
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<script src="WebServices.js" type='text/javascript'></script>
</head>
<body>
<div id="resultdiv"></div>
<script type='text/javascript'>
myAjax();
</script>
</body>
</html>
myAjax();
您需要一个CORS库作为服务的一部分。如果您现在需要它,请查看Thinktecture IdentityModel库:
否则,请等待WebAPI的下一版本:
您需要一个CORS库作为服务的一部分。如果您现在需要它,请查看Thinktecture IdentityModel库: 否则,请等待WebAPI的下一版本:
这正是我需要的。同时,祝贺您对ASP.net的贡献!这可不是一个小壮举!让我检查一下,一旦我能做到,我会给你的答案投票。对不起,我花了这么长时间才回复你。(我已经离开工作一段时间了。)不知何故,整个设置似乎有些过分。如果我只是尝试调用一个web服务方法并使用javascript处理结果,我不明白为什么需要所有服务器端代码。我想我应该提交一个新的问题,更充分地描述我的问题。原来我有多个问题。最大的问题是,正如Brock所说,我的web服务不允许跨源请求。我使用了Brock建议的WebAPI,这需要升级到Visual Studio 2012才能使用.Net 4.5。有点恼火。第二个问题是,当我在本地机器上使用Firefox17进行调试时,源代码为空。我通过使用HttpFox插件欺骗源代码解决了这个问题。这看起来正是我需要的。同时,祝贺您对ASP.net的贡献!这可不是一个小壮举!让我检查一下,一旦我能做到,我会给你的答案投票。对不起,我花了这么长时间才回复你。(我已经离开工作一段时间了。)不知何故,整个设置似乎有些过分。如果我只是尝试调用一个web服务方法并使用javascript处理结果,我不明白为什么需要所有服务器端代码。我想我应该提交一个新的问题,更充分地描述我的问题。原来我有多个问题。最大的问题是,正如Brock所说,我的web服务不允许跨源请求。我使用了Brock建议的WebAPI,这需要升级到Visual Studio 2012才能使用.Net 4.5。有点恼火。第二个问题是,当我在本地机器上使用Firefox17进行调试时,源代码为空。我通过使用HttpFox插件欺骗源代码解决了这个问题。