1

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.

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?

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
});
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Prithvi Raj Nandiwal
  • 3,122
  • 3
  • 22
  • 34

2 Answers2

0

Install nuget and then try these Here is the link of Json.Net on the nuget gallery: 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);

            } 
        } 
Dennis
  • 297
  • 6
  • 26
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..

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

AardVark71
  • 3,928
  • 2
  • 30
  • 50