0
votes

My Ajax call is not executing the web method and is returning status code "302 Found". Futher more... inspecting the trace shows that the method was actually sent as "OPTIONS"??

This was all working when the web method was in an aspx file. I had to move it to a asmx as I turned the code into a UserControl.

Ajax Call:

$.ajax({
            type: "POST", //HTTP method
            url: '<%= ResolveUrl("http://tempuri.org/PLService.asmx/getLocations")%>', //page/method name
            data: "{}", //json to represent argument
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: processData //handle the callback to handle response
        })

Web Method in PLService

    [WebMethod]
        public static string getLocations()
        {
            System.Diagnostics.Debug.WriteLine("Getting Locations.");
            return "{\"region\":\"auckland\", \"city\":\"auckland\"}";
        }

Request:

Host: tempuri.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8 Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate Origin: http://localhost:1968 Access-Control-Request-Method: POST Access-Control-Request-Headers: cache-control,pragma Connection: keep-alive Pragma: no-cache Cache-Control: no-cache

Response Header:

Host: tempuri.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8 Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate Origin: http://localhost:1968 Access-Control-Request-Method: POST Access-Control-Request-Headers: content-type Connection: keep-alive Pragma: no-cache Cache-Control: no-cache

1
The website ("tempuri.org" is real target!?!) is not correctly responding to the CORS request. With CORS, an OPTIONS request is sent first, to make sure the remote site allows the request.user2864740
Of course this becomes obvious now that it is pointed out. I've changed it to localhost and now I've opened up a whole new can of worms.Aaz

1 Answers

0
votes

3 Things:

  1. Have a valid target:

    [WebService(Namespace = "")]

  2. When a Web method is declared in an asmx it should not be declared static

  3. You need to configure the web service with the [ScriptService] attribute to handle calls from client side scripts.

    [System.Web.Script.Services.ScriptService]