0

I am trying to save data using web method. But it shows an error like method not found.

function InsertMasterCourse() {
    var data = {};
    data.Name = $('[id$=txtName]').val();
    $.ajax({
        type: 'POST',
        url: '<%= ResolveUrl("~/MasterService.asmx/InsertMasterCourse") %>',
        data: "{data:" + JSON.stringify(data) + "}",
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        async: true,
        success: function (response) {
            $('#txtName').val('');
        },
        error: function (response) {
            alert(response.statusText);
        }
    });

    return false;
}

In .asmx

[WebMethod()]
    [ScriptMethod()]
    public static void InsertMasterCourse(Master_CourseBLL data)
    {
        data.CollegeId = 1;
        data.Status = "Active";
        data.CreatedOn = DateTime.Now;
        data.UpdatedOn = DateTime.Now;
        data.Save(true);
    }

In my web.config I have add http get and post request as follows

<location path="MasterService.asmx">
<system.web>
  <webServices>
    <protocols>
      <add name="HttpGet"/>
      <add name="HttpPost"/>
    </protocols>
  </webServices>
</system.web>

If i check google chrome console, it shows error like InsertMasterCourse.aspx not found. .aspx added with my web service method. How to solve it.

Hisanth
  • 65
  • 2
  • 14
  • Try removing the ScriptMethod() attribute. This will cause it to accept only GET requests. https://stackoverflow.com/questions/941484/webmethod-vs-scriptmethod . Also as per this example, you may need the ScriptService() attribute on the ASMX class declaration: https://www.aspsnippets.com/Articles/Call-Consume-Web-Service-ASMX-using-jQuery-AJAX-in-ASPNet.aspx – ADyson Jun 13 '17 at 13:04
  • Yes, I have removed ScriptMethod(). but still getting not found error – Hisanth Jun 14 '17 at 08:00
  • did you ensure you have everything set up properly as per the second link I gave? – ADyson Jun 14 '17 at 08:52
  • Yes, I did all. Also Add In web.config – Hisanth Jun 14 '17 at 09:23

1 Answers1

0

try this -

function InsertMasterCourse() {
    var data = {};
    data.Name = $('[id$=txtName]').val();
    $.ajax({
        type: 'POST',
        url: '<%= ResolveUrl("~/MasterService.asmx/InsertMasterCourse") %>',
        data: JSON.stringify(data),
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        async: true,
        success: function (response) {
            $('#txtName').val('');
        },
        error: function (response) {
            alert(response.statusText);
        }
    });
    return false;
}

In .asmx use Newtonsoft.dll in your assemble reference

using System.NewtonSoft.Data;
[WebMethod()]
[ScriptMethod()]
public static void InsertMasterCourse(string data)
{
     Datatable dt = Newtonsoft.Json.JsonConvert.DeSerializeObject(data);
}
dferenc
  • 7,918
  • 12
  • 41
  • 49