【问题标题】:Jquery Ajax call not successfulJquery Ajax 调用不成功
【发布时间】:2014-07-21 02:02:57
【问题描述】:

我在 Javascript 和 jquery 中有以下代码

function abc() {

    alert("called 1");

    $.ajax({
        url: "abc.htm",
        context: document.body
    }).done(function () {
        alert("done");
    });
}

我的html按钮代码如下

<input type="button" value="click" onclick="return abc();" />

由于某种原因,ajax 调用不成功。我已将所需的 jquery 文件添加到页面中。

请帮忙谢谢。

【问题讨论】:

  • 然后添加一个失败处理程序,看看你得到了什么错误,像这样 -> jsfiddle.net/4AVQ5
  • 你会收到第一个警报,对吧?
  • 我不知道你在做什么,但函数头不正确。函数 abc 中的所有 ajax 是否应该
  • 是的,我得到了第一个警报
  • @adeneo 我已经添加了失败处理程序,但它没有被调用

标签: javascript jquery html asp.net ajax


【解决方案1】:

这样修改...

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert("called 1");
$.ajax({url:"abc.htm",success:function(result){
$("#div1").html(result);
}}).done(function(){alert("d")});
});
});
</script>
</head>
<body>

<div id="div1"></div>
<button>Get External Content</button>

【讨论】:

    【解决方案2】:

    您不需要向 html 标记的 onclick 处理程序返回任何内容。只需像在

    中一样调用 abc()
    <input type="button" value="click" onclick="abc();" />
    

    另外,如果您想要更精确的错误处理,那么您可以使用错误和成功 ajax 函数。

    function abc() {alert("called 1");
            $.ajax({
                url: "abc.htm",
                context: document.body,
                success: function (data, status, xhr ) {
                    console.log(data.SomeValue);
                },
                error: function (xhr, status, error) {
                    console.log(error);
                } 
            }).done(function () {
                alert("done");
            });
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多