weixin_33725515 2017-06-13 13:02 采纳率: 0%
浏览 42

找不到Ajax Web方法

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.

  • 写回答

1条回答 默认 最新

  • weixin_33720452 2018-02-19 12:05
    关注

    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);
    }
    
    评论

报告相同问题?