【问题标题】:Ajax web method not found未找到 Ajax Web 方法
【发布时间】:2017-06-13 13:02:05
【问题描述】:

我正在尝试使用网络方法保存数据。但它显示了一个错误,例如找不到方法。

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>

如果我检查 google chrome 控制台,它会显示错误,例如 InsertMasterCourse.aspx not found。 .aspx 添加到我的 Web 服务方法中。如何解决。

【问题讨论】:

  • 尝试删除 ScriptMethod() 属性。这将导致它只接受 GET 请求。 stackoverflow.com/questions/941484/webmethod-vs-scriptmethod 。同样根据此示例,您可能需要 ASMX 类声明中的 ScriptService() 属性:aspsnippets.com/Articles/…
  • 是的,我已经删除了 ScriptMethod()。但仍然找不到错误
  • 您是否确保按照我提供的第二个链接正确设置了所有内容?
  • 是的,我都做了。同时添加 web.config

标签: asp.net ajax


【解决方案1】:

试试这个 -

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

【讨论】:

    猜你喜欢
    • 2013-08-16
    • 2021-04-23
    • 1970-01-01
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    相关资源
    最近更新 更多