title: Call Cross Domain Asp.Net Web Service Using Jsonp
body: <p>I've tried countless examples and cannot get this to work.</p>
  
  <p>I'm trying to call a cross domain asp.net web service but get back the following error every time:</p>
  
  <blockquote>
    <p>jQuery18105929389187970706_1348249020199 was not called</p>
  </blockquote>
  
  <p>Here's my web service:</p>
  
  <pre><code>[WebService(Namespace = "http://www.mywebsite.org/")]
  [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]
  [ScriptService]
  public class DataService : System.Web.Services.WebService
  {
      [WebMethod]
      [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
      public string GetIncidentsByAddress()
      {
          return "It worked!";
      }
  }
  </code></pre>
  
  <p>My HttpModule to handle Json:</p>
  
  <pre><code> public class JsonHttpModule : IHttpModule
      {
          private const string JSON_CONTENT_TYPE = "application/json; charset=utf-8";
  
          public void Dispose()
          {
          }
  
          public void Init(HttpApplication app)
          {
              app.BeginRequest += OnBeginRequest;
              app.ReleaseRequestState += OnReleaseRequestState;
          }
  
          bool _Apply(HttpRequest request)
          {
              if (!request.Url.AbsolutePath.Contains(".asmx")) return false;
              if ("json" != request.QueryString.Get("format")) return false;
              return true;
          }
  
          public void OnBeginRequest(object sender, EventArgs e)
          {
              HttpApplication app = (HttpApplication)sender;
  
              if (!_Apply(app.Context.Request)) return;
  
              // correct content type of request
              if (string.IsNullOrEmpty(app.Context.Request.ContentType))
              {
                  app.Context.Request.ContentType = JSON_CONTENT_TYPE;
              }
          }
  
          public void OnReleaseRequestState(object sender, EventArgs e)
          {
              HttpApplication app = (HttpApplication)sender;
  
              if (!_Apply(app.Context.Request)) return;
  
              // apply response filter to conform to JSONP
              app.Context.Response.Filter =
                  new JsonResponseFilter(app.Context.Response.Filter, app.Context);
          }
      }
  
      public class JsonResponseFilter : Stream
      {
          private readonly Stream _responseStream;
          private HttpContext _context;
  
          public JsonResponseFilter(Stream responseStream, HttpContext context)
          {
              _responseStream = responseStream;
              _context = context;
          }
  
          //...
  
          public override void Write(byte[] buffer, int offset, int count)
          {
              var b1 = Encoding.UTF8.GetBytes(
                _context.Request.Params["callback"] + "(");
              _responseStream.Write(b1, 0, b1.Length);
              _responseStream.Write(buffer, offset, count);
              var b2 = Encoding.UTF8.GetBytes(");");
              _responseStream.Write(b2, 0, b2.Length);
          }
  
          //...
      }
  </code></pre>
  
  <p>My web.config for said HttpModule:</p>
  
  <pre><code>&lt;add name="JSONAsmx" type="JsonHttpModule, App_Code"/&gt;
  </code></pre>
  
  <p>And lastly my jQuery call:</p>
  
  <pre><code>&lt;script src="js/jquery.jmsajax.min.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script src="js/jquery-1.8.1.min.js" type="text/javascript"&gt;&lt;/script&gt;
  
  &lt;script type="text/javascript"&gt;
  
      $.jmsajaxurl = function(options) {
          var url = options.url;
          url += "/" + options.method;
          if (options.data) {
              var data = ""; for (var i in options.data) {
                  if (data != "")
                      data += "&amp;"; data += i + "=" +
               msJSON.stringify(options.data[i]);
              }
              url += "?" + data; data = null; options.data = "{}";
          }
          return url;
      };
  
      $(function() {
          var url = $.jmsajaxurl({
              url: "http://www.mywebsite.org/apps/IncidentReportingService/DataService.asmx",
              method: "GetIncidentsByAddress",
              data: {}
          });
  
          $.ajax({
              cache: false,
              dataType: "jsonp",
              success: function(data) { successCallback(data); },
              error:function(xhr, status, errorThrown) { debugger;},
              url: url + "&amp;format=json"
          });
  
      });
  
      function successCallback(data) {
          debugger;
          $.each(data, function(i, item) {
              $("#tweets ul").append("&lt;li&gt;" + item.text + "&lt;/li&gt;");
          });
      };
  </code></pre>
  
  <p></p>
  
  <p>Any ideas?</p>