我想弄清楚如何使一个常规的html页面(与javascript)从我使用Asp.Net在Visual Studio中创建的WebService获取数据。我的最后一个项目是将WebService托管在一个域中,结果将需要由另一个域中的html页面接收。
我真的很难弄清楚如何设置web服务,这样我的页面可以与它对话。
我一直在努力遵循:
http://www.html5rocks.com/en/tutorials/cors/toc-adding-cors-support-to-the-server
Ajax没有JQuery?
和视频:http://www.asp.net/web-forms/videos/how-do-i/how-do-i-create-and-call-a-simple-web-service-in-aspnet
但我似乎无法解决这些问题。当我尝试使用Firefox加载页面时,什么也没有返回。我几乎肯定这个问题与我试图使用的URL有关,但我不知道它应该是什么。
使用javascript从函数HelloWorld2获得返回值的最佳方法是什么?
提前感谢。
WebService
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;
}
}
}
我在web的httpProtocol中添加了访问控制头。像这样配置我的web服务项目
<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);
}
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>
您需要一个CORS库作为您服务的一部分。如果您现在需要它,那么请查看Thinktecture IdentityModel库:
http://brockallen.com/2012/06/28/cors-support-in-webapi-mvc-and-iis-with-thinktecture-identitymodel/否则等待WebAPI的下一个版本:
http://brockallen.com/2013/03/13/cors-open-source-contribution-to-asp-net-and-system-web-cors/http://aspnetwebstack.codeplex.com/wikipage?title=CORS%20support%20for%20ASP.NET%20Web%20API