找不到Ajax Web方法

时间:2017-06-13 13:02:05

标签: asp.net ajax

我正在尝试使用网络方法保存数据。但它显示的错误类似于未找到的方法。

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;
}

在.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);
    }

在我的web.config中,我添加了http get和post请求,如下所示

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

如果我检查谷歌Chrome控制台,它会显示错误,如找不到InsertMasterCourse.aspx。 .aspx添加了我的Web服务方法。如何解决它。

1 个答案:

答案 0 :(得分:0)

试试这个 -

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;
}

在.asmx 在汇编参考中使用Newtonsoft.dll

using System.NewtonSoft.Data;
[WebMethod()]
[ScriptMethod()]
public static void InsertMasterCourse(string data)
{
     Datatable dt = Newtonsoft.Json.JsonConvert.DeSerializeObject(data);
}