weixin_33743703 2015-10-04 00:29 采纳率: 0%
浏览 44

新手jquery / ajax:为什么会出错?

I've made this simple html page but get "error" alert popup when calling example.com. Why?

<!DOCTYPE html>
<html>
    <body>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.min.js"></script>
        <script type="text/javascript">
            function errorAlert(e, jqxhr) {
                alert("Your request was not successful: " + jqxhr);
            }
            function processData() {
                alert("success")
            }
            $.ajax(
            {
                type: "post",
                url: "http://example.com",
                success: processData,
                error: errorAlert
            }); //end of $.ajax
            </script>
    </body>
</html>

I'm running on Chrome 45.0...

  • 写回答

1条回答 默认 最新

  • ?Briella 2015-10-04 00:36
    关注

    You can see different types of JavaScript errors in the developer console (F12 in Google Chrome). You will see: "XMLHttpRequest cannot load http://example.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access."

    Take a look here for a detailed explanation: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '...' is therefore not allowed access

    But basically, the host you send the request to must match the origin, or explicitly allow foreign hosts.

    评论

报告相同问题?