[英]CORS ASP.NET webservice from javascript
I'm trying to figure out how to make a regular html page (with javascript) get data from a WebService which I created in Visual Studio using Asp.Net. 我试图弄清楚如何使一个常规的html页面(使用javascript)从我在Visual Studio中使用Asp.Net创建的Web服务中获取数据。 My final project is going to have the WebService hosted in one domain and the results will need to be received by the html page in another domain.
我的最后一个项目将把WebService托管在一个域中,并且结果将需要由另一个域中的html页面接收。
I'm having a really hard time figuring out how to set up the webservice such that my page can talk to it. 我很难弄清楚如何设置网络服务,以便我的页面可以与之对话。
I've been trying to follow: 我一直在尝试遵循:
http://www.html5rocks.com/en/tutorials/cors/#toc-adding-cors-support-to-the-server http://www.html5rocks.com/zh-CN/tutorials/cors/#toc-adding-cors-support-to-the-server
Ajax Without JQuery? 没有JQuery的Ajax?
and the video here: http://www.asp.net/web-forms/videos/how-do-i/how-do-i-create-and-call-a-simple-web-service-in-aspnet 此处的视频: http : //www.asp.net/web-forms/videos/how-do-i/how-do-i-create-and-call-a-simple-web-service-in-aspnet
but I can't seem to work out the kinks. 但我似乎无法解决问题。 When I try to load the page using Firefox nothing is returned.
当我尝试使用Firefox加载页面时,不会返回任何内容。 I'm almost positive that the problem has to do with the URL I'm trying to use, but I don't know enough to figure out what it should be.
我几乎肯定这个问题与我要使用的URL有关,但是我还不知道该怎么做。
What is the best way to get the return value from the function HelloWorld2 using javascript? 使用JavaScript从函数HelloWorld2获取返回值的最佳方法是什么?
Thanks in advance. 提前致谢。
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;
}
}
}
I added access control headers to the httpProtocol in the web.config of my web services project like this 我将访问控制标头添加到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: 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 Code: 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>
You need a CORS library as part of your service. 您需要将CORS库作为服务的一部分。 If you need it now, then look into the Thinktecture IdentityModel library:
如果现在需要,请查看Thinktecture IdentityModel库:
http://brockallen.com/2012/06/28/cors-support-in-webapi-mvc-and-iis-with-thinktecture-identitymodel/ http://brockallen.com/2012/06/28/cors-support-in-webapi-mvc-and-iis-with-thinktecture-identitymodel/
Otherwise wait for the next release of WebAPI: 否则,请等待下一个WebAPI版本:
http://brockallen.com/2013/03/13/cors-open-source-contribution-to-asp-net-and-system-web-cors/ 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 http://aspnetwebstack.codeplex.com/wikipage?title=CORS%20support%20for%20ASP.NET%20Web%20API
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.