简体   繁体   English

来自JavaScript的CORS ASP.NET Web服务

[英]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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用javascript从asp.net webservice中使用Json数据 - How to consume Json data from asp.net webservice with javascript 如何将字符串数组从 javascript 传递到 asp.net webservice? - How to pass a string array from javascript to asp.net webservice? Javascript调用Web服务和ASP.Net序列 - Javascript calling webservice and ASP.Net Sequence asp.net-&gt; javascript-&gt; webservice-&gt;从webservice调用返回两个类 - asp.net -> javascript -> webservice -> Returning two classes from webservice call 在从JavaScript到ASP.NET WebService的调用过程中,如何将“ NULL”值传递给可为空的字段 - How to pass “NULL” value into nullable field during call from JavaScript to ASP.NET WebService 测试从某些JavaScript返回JSON的ASP.NET Webservice的最简单方法是什么? - Simplest way to test ASP.NET Webservice that returns JSON from some JavaScript? 无法在JavaScript中附加从asp.net网络服务返回的数组值 - Can't append the array value returned from asp.net webservice in javascript 在JavaScript循环中调用ASP.NET Web服务会导致问题 - Calling ASP.NET Webservice in javascript loop causes problems Javascript-调用ASP.NET WebService-服务器方法&#39;methodName&#39;失败 - Javascript - calling ASP.NET WebService - The server method 'methodName' failed JavaScript中的WebService无法在asp.net中运行 - WebService in JavaScript doesn't working asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM