azamsharp
azamsharp

Reputation: 20068

Calling ASMX Service from jQuery Client Side Code

I have a VS 2010 project locally where I have a ASMX service running locally. I created another VS 2010 project which has to consume that local ASMX service. For some reason whenever I trigger the service it gives me 500 Internal Service Error. Both applications are running on separate ports.

 $.ajax(

    {
        type: "POST",
        url: "http://localhost:22059/Mobile/HOCWebService.asmx/GetCategories",
        data: "{}",
        dataType: "json",
        contentType:"application/json",
        success: function (response) {

            alert(response); 

        }
    }

    );

Upvotes: 1

Views: 251

Answers (2)

Chandra
Chandra

Reputation: 11

MAKE SURE THAT YOUR ADDING THE FOLLOWING LINES OF CODE ABOVE THE WEBSERVICE CLASS

[System.Web.Script.Services.ScriptService]

AFTER ADDING THE CODE LOOK LIKE BELOW

[System.Web.Script.Services.ScriptService]
public class WSDataService : System.Web.Services.WebService

THIS ABOVE LINE OF CODE WILL ALLOW THE AJAX CLINET CALL TO CONSUME WEBSERVICE.

THEN YOUR CLIENT WILL START COSUMING

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

Both applications are running on separate ports.

That's the problem. You are violating the same origin policy restriction. You cannot send AJAX requests to services that are not hosted on the same origin as the page containing the script (different ports => different domains).

Upvotes: 2

Related Questions