duan19911992 2015-05-06 04:46
浏览 34
已采纳

jquery调用php无法正常工作

Hi I am a complete noob in this. I am trying to call a simple php page from js script

$.ajax({
            url: "././mail/testphp.php",
            type: "POST",
            dataType: "html",
            success: function() {
                // Success message
                alert("success");
            },
            error: function(xhr,textStatus,err) {
                alert("readyState: " + xhr.readyState + "
" +
                      "responseText: " + xhr.responseText + "
" +
                      "status: " + xhr.status + "
" +
                      "text status: " + textStatus + "
" +
                      "error: " + err);
            },
        })

This is the testphp:

<?php
echo 'Hello';
?>

The alert I am getting is "NetworkError: Failed to execute send on XMLHttpRequest failed to load file dir/testphp" (the directory is correct).

Both js and php are on my local hard drive. Do I need to put it on a server for it to work or something?

edit: here's the error I am getting (note: the directory IS correct) enter image description here

  • 写回答

6条回答 默认 最新

  • doufen3563 2015-05-06 07:08
    关注

    Are you loading all files (html and js) from file? (e.i. file://....). If you are loading them through a local webserver, you may face a cross site scripting block from your browser. You can easily find such block if you use google chrome and open the developer tools.

    You then can start chrome with a special flag to circumvent this problem (see Chrome Allow File Access From File. However, I reccommend to install a local webserver you can use for testing.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?