简体   繁体   English

Ajax简单的例子不起作用

[英]Ajax simple example not working

I haven't used ajax in a while and now I can't even get this simple program to work, am I doing something wrong ? 我有一段时间没用过ajax,现在我甚至无法让这个简单的程序工作,我做错了什么?

        <script type = "text/javascript" src = "jquery-1.9.1.min.js"></script>
    <script type = "text/javascript">
        alert("");
        var count = 0;
        $.ajax({
               url: 'get.php',
               dataType: 'json',
               success: function () 
               {       
                alert("");
               } 
        });
        alert("");
    </script>

<?php
echo "yay";
?>

Thanks 谢谢

Setting the dataType as "json" means the response from get.php is parsed as JSON. dataType设置为“json”意味着get.php的响应被解析为JSON。 If it's not valid JSON or the response is empty, the request will fail. 如果它无效JSON或响应为空,则请求将失败。

If the URL is incorrect (can't be found...HTTP 404 error), then the request will fail. 如果URL不正确(无法找到... HTTP 404错误),则请求将失败。

The default type of request is "GET", so if get.php doesn't allow "GET" (for some reason), it will return an HTTP error, and the request will fail. 默认的请求type是“GET”,因此如果get.php不允许“GET”(由于某种原因),它将返回HTTP错误,请求将失败。

If there's an error on the server, it will likely return an HTTP 500 error, and the request will fail. 如果服务器上出现错误,则可能会返回HTTP 500错误,请求将失败。

Something to help debug would be to add the error option to the $.ajax call and see if that's called. 帮助调试的一点是将error选项添加到$.ajax调用中,看看是否调用了它。 Instead, I use the .fail() method...it does the same thing. 相反,我使用.fail()方法...它做同样的事情。

Of course, the more direct way of debugging is opening your browser's console and viewing the AJAX request. 当然,更直接的调试方法是打开浏览器的控制台并查看AJAX请求。 It should show multiple details about it, that can help you determine any problems. 它应该显示有关它的多个细节,可以帮助您确定任何问题。

It might seem as if the AJAX request was never executed/sent, because you don't see the alert in the middle. 看起来AJAX请求似乎从未被执行/发送,因为您没有在中间看到alert Well, just because the request wasn't successful, doesn't mean it was skipped. 好吧,仅仅因为请求不成功,并不意味着它被跳过了。 There are plenty of reasons (I named several above) why the request may fail. 有很多原因(我在上面提到了几个)为什么请求可能会失败。 And the .fail() method will help you determine the cause. .fail()方法将帮助您确定原因。

Also, the universal convention for handling deferred objects in jQuery is to use the done and fail methods, so that is an option. 此外,在jQuery中处理延迟对象的通用约定是使用donefail方法,因此这是一个选项。 Of course, $.ajax has specific options you can specify ( success , error , and complete - which is for something else), so that is also an option. 当然, $.ajax有你可以指定的特定选项( successerrorcomplete - 这是其他的东西),所以这也是一个选项。 You can also use special methods ( .success() , .error() , .complete() ) that are part of the object returned from $.ajax , but those are deprecated as of version 1.8 - take a look at the .ajax docs towards the bottom - http://api.jquery.com/jQuery.ajax/ . 您还可以使用特殊的方法( .success() .error() .complete()是对象的一部分,从返回$.ajax ,但那些被弃用的1.8版本-看看在.ajax底部的文档 - http://api.jquery.com/jQuery.ajax/ But here's how I'd set it up, which shouldn't be any different from yours, but does catch errors: 但这是我如何设置它,它应该与你的不同,但确实会发现错误:

$.ajax({
    url: 'get.php',
    dataType: 'json'
}).done(data) {
    console.log("successful response");
}).fail(jqXHR, textStatus, errorThrown) {
    console.log("error: " + textStatus + ", " + errorThrown);
});

Are you sure jQuery has loaded by the time your ajax call is hit? 你确定jj在你的ajax调用被击中时已经加载了吗?

Try wrapping it in a doc ready to make sure you are actually loaded 尝试将其包装在doc中,以确保您确实已加载

$(document).ready(function () {
    alert("");
    var count = 0;
    $.ajax({
        url: 'get.php',
        dataType: 'json',
        }).alwaysfunction () {
           alert("call was hit");
        });
    });

});

If there's a problem with get.php, your code will not indicate any problems, jquery.ajax() just swallow the error. 如果get.php有问题,你的代码不会指出任何问题,jquery.ajax()只是吞下错误。

Even before adding the .failure callback routine to your ajax call, you should be able to check the error log on web server executing get.php, any errors related to executing get.php will be logged. 甚至在将.failure回调例程添加到ajax调用之前,您应该能够检查执行get.php的Web服务器上的错误日志,将记录与执行get.php相关的任何错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM