Call asmx web method from master page

1.7k views Asked by At

My Project Structure

Web Dir
 - App_code
 - App_Data
 - DevFolder 
     - LiveFolder
         - Live.aspx
 - Dev.master
 - DevMasterEvents
     - masterservice.asmx

In master page,

 $.ajax({
            type: "POST",
            url: "DevMasterEvents/masterservice.asmx/HelloWorld",             
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: SetTabSessionValueSucceed,
            error: SetTabSessionValueFailed
        });

function SetTabSessionValueSucceed(result) {
        alert("text from server: " + result.d);
    }

function SetTabSessionValueFailed() {
        alert('call failed');
    }

Web Method in masterservice.asmx

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

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }
}

It throws call failed. Please help

Edit: I am afraid the reason could https://stackoverflow.com/a/12621912/2922388 as I can the entire page refreshes (not sure why).

Yes, it was my careless mistake :(

 <span class="input-group-btn">
 <button type='button' name='search' id='search-btn' class="btn btn-flat"><i class="fa fa-search"></i></button>
 </span>

The earlier type was 'submit' I changed to button. Now I receive "404 error Not Found" .

1

There are 1 answers

6
Habib On BEST ANSWER

The problem is due to content type and data type, just remove them from Ajax request:

$.ajax({
            type: "POST",
            url: "DevMasterEvents/masterservice.asmx/HelloWorld",             
            success: SetTabSessionValueSucceed,
            error: SetTabSessionValueFailed
        });

Also you are ignoring the error in SetTabsessionValueFailed instead just displaying a message, which doesn't help you in debugging it. See: How do you handle errors from AJAX calls?

Aside from that see: More Reasons to Not use ASMX Services in New Code