【问题标题】:jQuery Ajax Get wont workjQuery Ajax Get 不起作用
【发布时间】:2015-02-22 11:05:01
【问题描述】:

我有这个 html 表单,我需要从控制器获取一些结果(如布尔结果)...... 这就是我的表单的样子......

<div id="temp"></div>
@using (Html.BeginForm("Result", "Home"))
{
    @Html.TextBox("str")
    <input type="submit" value="Submit" id="submit">
}

这就是我的 Get Method 和 Post Method 在控制器中的样子......

public ActionResult Index()
{
    return View();
}
[HttpPost]
public ActionResult Index(string str)
{
   bool res = true;
   if(str == "Jude")
         res=true;
    Return View(res);
} 

所以现在我需要做的是,我需要检查结果是真还是假,并从 html 页面给用户一个确认........

<script>
    $('#submit').click(function () {
        alert("hello");
        $.get("../Home/Index", function (data) {
            var res = html(data);
            if (res == true)
                $('#temp').replaceWith("Success");
            else
                $('#temp').replaceWith("Some Thing Went Wrong!!");                
        });

        //$.ajax({
        //    type: "GET",
        //    url: "../Home/Index",
        //    data: data,
        //    success: function (data)
        //    {
        //        $('#temp').append("hello");
        //    },
        //    dataType: dataType
        //});

    });
</script>

请告诉我如何解决这个问题....... 谢谢!!!!

【问题讨论】:

    标签: javascript jquery ajax jquery-ui javascript-events


    【解决方案1】:

    以 JSON 格式从控制器返回数据,例如。你的输出应该像 {"res": true}

    然后在js端: ... 如果(data.res == true) ...

    【讨论】:

      猜你喜欢
      • 2017-02-20
      • 2012-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多