【问题标题】:Ajax calling WebMethod nothing happensAjax 调用 WebMethod 没有任何反应
【发布时间】:2014-12-22 16:35:55
【问题描述】:

为什么哦,为什么这段代码什么都不做?!我的ajax有什么问题? 我的 ajax:

$.ajax({
                type: "POST",
                url: "/index.aspx/Uppdatera",
                data: "{ Id:"+ JSON.stringify($item.Id) +" }",
                error: 
                    alert("error")
            });

我的 C#:

 [WebMethod(EnableSession = true)]
        public static void Uppdatera(object id)
        {
            id.ToString();
            using (StreamWriter s = new StreamWriter("C:/Users/robert/Documents/visual studio 2013/Projects/GolvProjektet/GolvProjektet/hej.txt"))
            {
                s.Write("hej" + id);
            }
        }

【问题讨论】:

  • 您必须使用 Firebug 并点击“net”并阅读您的 ajax 请求和响应。
  • 我确定有事,你调试了吗?您是否检查过诸如提琴手或浏览器开发工具之类的东西以查看请求在做什么?
  • 这是什么意思?如果您不解释所需的行为是什么、它正在做什么以及您已经尝试过什么,我们将无法提供帮助。
  • 目前,我只是想让 WebMethod 工作,然后我将研究它将做什么:) 我单击一个按钮,然后我的 ajax 正在运行。但是没有创建文件 hej.txt。
  • @prospector 我收到错误代码 301:永久移动。

标签: c# jquery ajax webmethod


【解决方案1】:

在ajax调用中添加contentType y dataType:

$.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                url: "/index.aspx/Uppdatera",
                data: "{ Id:"+ JSON.stringify($item.Id) +" }",
                error: 
                    alert("error")
            });

【讨论】:

  • 仍然没有创建文件。
猜你喜欢
  • 2017-02-22
  • 2018-01-09
  • 2012-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-27
相关资源
最近更新 更多