如何创建跨域asp.net Web服务

时间:2022-02-24 03:14:11

I have created a web service. I want to access this web service using Ajax jQuery. I am able to access on same domain. But I want to access this web service from another domain.

我创建了一个Web服务。我想使用Ajax jQuery访问此Web服务。我可以访问同一个域。但我想从另一个域访问此Web服务。

Does anyone have an idea how to create a cross domain web service in asp.net? Any setting in web.config file so that I access it from another domain?

有谁知道如何在asp.net中创建跨域Web服务? web.config文件中的任何设置,以便我从另一个域访问它?

My web service

我的网络服务

[WebService(Namespace = "http://tempuri.org/")]
[System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
    public Service () {
    }

    [WebMethod]
    public string SetName(string name) {
        return "hello my dear friend " + name;

    }
}

JavaScript

$.ajax({
    type: "GET",
    url:'http://192.168.1.119/Service/SetName.asmx?name=pr',
    ContentType: "application/x-www-form-urlencoded",
    cache: false,
    dataType: "jsonp",
    success: onSuccess
});

2 个解决方案

#1


0  

the use of jQuery's jsonp datatype is indeed a good choice as it will make cross-domain scripting possible... but since this is json-with-padding you must make sure your webservice returns json..

使用jQuery的jsonp数据类型确实是一个不错的选择,因为它可以使跨域脚本编写成为可能......但由于这是json-with-padding,你必须确保你的webservice返回json ..

Check for instance here for a good starter on that: http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/

例如,在这里查看一个好的启动器:http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/

#2


0  

Install nuget and then try these Here is the link of Json.Net on the nuget gallery: nuget.org/packages/Newtonsoft.Json

安装nuget,然后尝试这些这是在nuget画廊上的Json.Net的链接:nuget.org/packages/Newtonsoft.Json

using Json;
using Newtonsoft.Json;
using System.Xml.Serialization;


     [WebService(Namespace = "http://tempuri.org/")] 
        [System.Web.Script.Services.ScriptService] 
        public class Service : System.Web.Services.WebService 
        { 
            public Service () { 
            } 

            [WebMethod] 
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
            public string SetName(string name) { 
         return JsonConvert.SerializeObject("hello my dear friend " + name, Newtonsoft.Json.Formatting.Indented);

            } 
        } 

#1


0  

the use of jQuery's jsonp datatype is indeed a good choice as it will make cross-domain scripting possible... but since this is json-with-padding you must make sure your webservice returns json..

使用jQuery的jsonp数据类型确实是一个不错的选择,因为它可以使跨域脚本编写成为可能......但由于这是json-with-padding,你必须确保你的webservice返回json ..

Check for instance here for a good starter on that: http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/

例如,在这里查看一个好的启动器:http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/

#2


0  

Install nuget and then try these Here is the link of Json.Net on the nuget gallery: nuget.org/packages/Newtonsoft.Json

安装nuget,然后尝试这些这是在nuget画廊上的Json.Net的链接:nuget.org/packages/Newtonsoft.Json

using Json;
using Newtonsoft.Json;
using System.Xml.Serialization;


     [WebService(Namespace = "http://tempuri.org/")] 
        [System.Web.Script.Services.ScriptService] 
        public class Service : System.Web.Services.WebService 
        { 
            public Service () { 
            } 

            [WebMethod] 
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
            public string SetName(string name) { 
         return JsonConvert.SerializeObject("hello my dear friend " + name, Newtonsoft.Json.Formatting.Indented);

            } 
        }