【问题标题】:Why network response is empty?为什么网络响应为空?
【发布时间】:2016-05-30 14:46:51
【问题描述】:

我有一个简单的任务要做。我将名称写入输入,然后单击按钮,最后我应该显示带有“hello name!”的警报窗口文本,但我的警报窗口是空的。我必须使用控制器来执行此操作,因为稍后我将使用数据库中的数据。我的代码:

控制器

public JsonResult Getname(string name)
    {
        string text = "Hello " + name;
        return Json(text, JsonRequestBehavior.AllowGet);
    }

查看

$("#btnGetText").click(function () {
    var name = $("#txtName").val();
    GetText(name);
});

function GetText(e) {
    $.ajax({
        url: '@Url.Action("GetText","Home")',
        type: "GET",
        data: { "name": e },
        success: function (data) {

            alert("It works");
            alert(data);

        },
        error: function () {
            alert("Error");
        }
    });
};

我认为问题在于我给出了太多的回应,我不知道为什么。 enter image description here

【问题讨论】:

  • 我对asp.net一无所知,但操作不应该是Getname吗?
  • 改了还是不行

标签: c# jquery json asp.net-mvc


【解决方案1】:

在你的 js 中:

@Url.Action("GetText","Home")

但是您的控制器名称是Getname。应该是一样的。在控制器端或js端更改它。

【讨论】:

  • @smile 也将dataType: "json", 添加到您的 json 请求中
  • 还是什么都没有
猜你喜欢
  • 2020-08-22
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 2015-07-25
  • 1970-01-01
  • 2011-10-06
  • 2012-09-26
  • 1970-01-01
相关资源
最近更新 更多