0

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" .

Community
  • 1
  • 1
Gopi
  • 5,656
  • 22
  • 80
  • 146

1 Answers1

2

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

Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
  • I am still getting the same error. When I tried to debug the error by following "How do you handle errors from ajax calls", it throws "Request Status: 0 Status text: error undefined" – Gopi Jun 09 '15 at 14:30
  • I even converted the web service as wcf, still the same error. But when I invoke the wcf or web service separately, debugger hits the break point which is on the method – Gopi Jun 09 '15 at 14:44
  • I've updated my questions, please have look and share your thoughts – Gopi Jun 09 '15 at 14:51
  • @TechJerk, show the code where you are sending the Ajax request, looks like you are triggering some server side event as well. How and where are you making call to ajax in your aspx code ? – Habib Jun 09 '15 at 14:53
  • @TechJerk, There shouldn't be 404 error as long as your project structure is as you described. Try `http://localhost/DevMasterEvents/masterservice.asmx` in browser and see if you are getting to your service, If you are debugging it from Visual studio then use port number with local host. – Habib Jun 09 '15 at 15:29
  • Yes, it worked. I used firebug to identify the 404 issue. Thanks – Gopi Jun 09 '15 at 15:31