0

I have created a webservice in one application.Now i need to consume that in my another website .I know how it can be done if the webservice is in the same application like providing the url like "webservicename.asmx/getInventoryForWs" using jquery. my jquery call code is

     $.ajax({
            type: "POST",
            url: "http://localhost/DashboardUI/Reports/Marketing/AUSUWs.asmx/getInventoryForWs",
            contentType: "application/json; charset="utf-8",
            dataType: "jsonp",
            success: function (ret) {
                alert('success');
            },
            error: function (httpRequest, textStatus, errorThrown) {
                alert("status=" + textStatus + ",error=" + errorThrown);
            }
        });    

Here is my vb.net webservice code

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Web.Script.Services

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the       
following line.
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://microsoft.com/webservices/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class AUSUWs
Inherits System.Web.Services.WebService

<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Function getInventoryForWs() As String
    Try
        Dim a As New AUSUReport
        Dim dt As DataTable = a.getInventoryForWs().Tables(0)
        Return (GetJson(dt))
    Catch ex As Exception
        Throw ex
    End Try
End Function
Public Function GetJson(ByVal dt As DataTable) As String
    Dim serializer As New System.Web.Script.Serialization.JavaScriptSerializer()
    Dim rows As New List(Of Dictionary(Of String, Object))()
    Dim row As Dictionary(Of String, Object) = Nothing

    For Each dr As DataRow In dt.Rows
        row = New Dictionary(Of String, Object)()
        For Each col As DataColumn In dt.Columns
            row.Add(col.ColumnName.Trim(), dr(col))
        Next
        rows.Add(row)
    Next
    Return serializer.Serialize(rows)
End Function
End Class

can anyone help me with this?

1 Answers1

0

I think this part is not correct with double slahes:

 url: "http://somewebsite.com//
       DashboardUI/Reports/Marketing/AUSUWs.asmx/getInventoryForWs", 

Try this:

 url: "http://somewebsite.com/
       DashboardUI/Reports/Marketing/AUSUWs.asmx/getInventoryForWs", 

Edit:

"dataType should be the type of what you receive but contentType should be the mime-type of what you are sending, the following should be ok"

JQuery.ajax({ 
    type: "POST", 
    url: "http://somewebsite.com/DashboardUI/Reports/Marketing/AUSUWs.asmx/getInventoryForWs",       
    dataType: "json", 
    success: function (ret) { 
              $(ret).find("Table").each(function () 
               { 
                   alert('success'); 
                   // $('#').val($(this).find('').text());
                });
        },
        error: function (httpRequest, textStatus, errorThrown) {
            alert("status=" + textStatus + ",error=" + errorThrown);
        }
    });

Here is a detailed walkthrough. Do everything exact way like here and you won't have problem: Introduction to using jQuery with Web Services (VB.Net)

speti43
  • 2,886
  • 1
  • 20
  • 23
  • you post your data in xml to the service, and you get response in json? – speti43 Jul 04 '13 at 06:35
  • ya, i need to load some datas from my external web service.Do you have any solution? –  Jul 04 '13 at 06:36
  • by the way data is plural by default so there is no word such as "datas" – speti43 Jul 04 '13 at 06:37
  • can you get a more detailed exception from firebug? https://getfirebug.com/errors – speti43 Jul 04 '13 at 06:42
  • I tried with your code,but still problem remains $.ajax({ type: "POST", url: "http://localhost/DashboardUI/Reports/Marketing/AUSUWs.asmx?getInventoryForWs", contentType: "application/xml; charset=utf-8", dataType: "json", success: function (ret) { alert('success'); }, error: function (httpRequest, textStatus, errorThrown) { alert("status=" + textStatus + ",error=" + errorThrown); } }); –  Jul 04 '13 at 08:22
  • update you question with the new parameters what is localhost? use http before it – speti43 Jul 04 '13 at 08:29
  • localhost should have a specific port, just try in your browser, if it's not loading in, it won't work with ajax call. Does this loading in your browser? http://localhost/DashboardUI/Reports/Marketing/AUSUWs.asmx/ – speti43 Jul 04 '13 at 08:55
  • Ya it is loading and that is the web service url which i created in vb.net –  Jul 04 '13 at 09:00
  • Ok, but you didn't pass anything to the method. You don't have to specify the contenttype just remove it. I added to my code this modification. – speti43 Jul 04 '13 at 09:24
  • my service is not hosted in the same web application path. And my code will work if my web services and my web application hosted in the same domain.But i need to do a cross-domain AJAX call –  Jul 04 '13 at 09:41
  • Exactly the same webservice should work cross-domain also, so there is something difference. Unless the consumer uses ssl, https, but the service owner don't provides. – speti43 Jul 04 '13 at 09:44
  • So what shall i do now?Any solution?Thanks in advance –  Jul 04 '13 at 09:46
  • If I don't see the service, I can't help, but I think you can't provide it. So in that case you have to find the solution, I have no further idea. There must be a configuration error on the live service. If the live service is managed by another person, maybe you can ask for help. – speti43 Jul 04 '13 at 09:50
  • You are not correct.My web service is working fine and it is showing the output as well and i have placed all of my service codes above.May i know what you are asking then ? –  Jul 04 '13 at 09:57