控制台对 jquery ajax 请求没有响应

No response in console to jquery ajax request

我正在对 codeigniter php 函数进行以下 jquery ajax 调用:

    $.ajax({
                        type:"POST",
                        url: "AjaxUpdate/getHtml",
                        data:{ u : 'http://whosebug.com/' },
                        dataType: 'json',
                        success: function(data) {
                            console.log(data);

                        },

                       error: function(jqXHR, textStatus, errorThrown) {
                                console.log('error');
                                console.log(jqXHR,textStatus, errorThrown);
                        }
                    });

请求的php函数是:

public function getHtml() {

        $url = $_POST['u'];
        $result = file_get_contents($url);
        return json_encode($result);
}

我该如何解决这个问题?

编辑:控制台中的错误显示:

error
reply_detail:4667 Object {readyState: 4, getResponseHeader: function, getAllResponseHeaders: function, setRequestHeader: function, overrideMimeType: function…} "parsererror" SyntaxError: Unexpected end of input {stack: (...), message: "Unexpected end of input"}message: "Unexpected end of input"stack: (...)get stack: function () { [native code] }set stack: function () { [native code] }__proto__: Error

您确定是在文件中调用函数吗? 您需要通过 调用 来调用函数。

getHtml();

有些事情可能正在发生。

我不知道 PHP,但是 getHtml 真的会返回 JSON 吗?

可能是返回内容解析失败。 JQuery$.ajaxdataType属性中还有其他选项。

它没有执行 success 函数,因为它可能在某些时候失败。尝试添加一个 error 函数并记录数据。 JQuery documentation 非常有帮助和丰富。

还有,试试改成data: JSON.stringify({ u : 'http://whosebug.com/' }),至少在ASP.NET我每次都得改