1

I have hosted a web-service in let

http://example1.com/webservice.asmx

and want to call from

http://example2.com

i have Code of jQuery like in example2.com

GetData: function () {
    $.ajax({
        crossDomain: true,
        type: "POST",
        url: "http://example1.com/webservice.asmx/GetData",
        dataType: "jsonp",
        contentType: "application/json; charset=utf-8",
        data: { Date: '' },
        success: function (data, textStatus, jqXHR) {
            debugger;                
        },
        error: function (data, textStatus, jqXHR) {
            alert("data");
        }
    });
  }

it hit url like

http://example1.com/webservice.asmx/GetData?callback=jQuery19106349606812515739_1396429620115&Date=&_=1396429620116

and it hit that url by using GET Method (From firebug). actually where is problem i am unable to find it. it response data in XML format. and also responsed data in XML Format but not get in success event . but it work fine if i put same code on same domain.

manoj
  • 5,235
  • 7
  • 24
  • 45
  • 1
    did your webservice wrap the response with the callback? e.g `jQuery19106349606812515739_1396429620115({'iam':'the json response'});`? – Jan Hommes Apr 02 '14 at 09:19
  • no , i don't know from where jQuery19106349606812515739_1396429620115 is added to requested to service url. – manoj Apr 02 '14 at 09:21
  • Do you know what `dataType: "jsonp"` mean? – Mat J Apr 02 '14 at 09:25

2 Answers2

0

For JSONP your response must be wrapped in a Javascript function. Jquery automatically adds a callback-parameter to the GET-URL if you set dataType: "jsonp". This callback parameter is a random js function name, which your ajax request needs to revive the data cross origin.

To make it work you need to change your Webservice as the following:

  1. change to JSON
  2. wrap the response with the callback-parameter. For example in VB.NET:

    Dim returnVal=Request.Param("Callback") & "(" & jsonreturn & ");"
    Response.Write(returnVal) 
    

See this post for details to get it working with your asmx webservice. Alternative you can change your Webservice to CORS.

Community
  • 1
  • 1
Jan Hommes
  • 5,122
  • 4
  • 33
  • 45
0

This fix works for me with pure json call from JQuery. Enable HttpGet and Post from your web.config within system.web

<webServices>
  <protocols>
    <add name="HttpSoap"/>
    <add name="HttpPost"/>
    <add name="HttpGet"/>
    <add name="HttpPostLocalhost"/>
  </protocols>
</webServices>

Afterwards, create a method in your Global.asax file to enable cross domain communication (vb.net) '==========================EnableCrossDmainAjaxCall=================

Private Sub EnableCrossDmainAjaxCall()
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*")

    If HttpContext.Current.Request.HttpMethod = "OPTIONS" Then 
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS")
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept")
        HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000")

        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "origin, x-requested-with, content-type") 
        HttpContext.Current.Response.[End]()
    End If

End Sub

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache)
    HttpContext.Current.Response.Cache.SetNoStore()
    EnableCrossDmainAjaxCall() 
End Sub
'==========================
Ammog
  • 124
  • 3