簡體   English   中英

帶有jQuery的基本Ajax-執行PHP文件

[英]Basic Ajax with jQuery - Execute PHP File

由於某種原因,我無法使它正常工作。 我要做的就是單擊一個按鈕時(通過ajax)執行一個php文件。 aka,如果我不使用ajax,我們將研究:

<a href="file.php">dfgdfg</a>

我希望在不離開頁面的情況下執行文件。

這是我的atm:

$(".vote-up").live('click', function(e) {
$.ajax("file.php", function(){
       alert("Executed file");
   });
});

這似乎不起作用。 我真的很困惑jQuery ajax函數在一般情況下如何運行,而不處理任何復雜的遠程事件。

補充問題:

.ajax({
       url: 'includes/login-check-jquery.php',
       success: function (data) {
            if(data == 1){
                alert("Logged in!!!");
            }else{
                window.location = data;
            }
        error: function (xhr, status, error) {
        // executed if something went wrong during call
        if (xhr.status > 0) alert('Error: ' + status); // status 0 - when load is interrupted
        }
    });
});

在上面的代碼中,如果返回的數據等於“已登錄”,則會出現一個消息框。 否則,它將重定向到該位置(以數據發送)。 由於某種原因,該if語句不起作用,但是數據按預期返回。

我的PHP代碼是:

<?php  
if (!$user->logged_in){
    echo "../login/index.php";
}else{
    echo "1";
}

?>

有任何想法嗎?

如果您不想離開頁面,請執行

$(".vote-up").live('click', function(e) {
    e.preventDefault();
    ...

如果需要,可以更新ajax

$.ajax({
    url: 'file.php',
    success: function (data) {
        // this is executed when ajax call finished well
        alert('content of the executed page: ' + data);
    },
    error: function (xhr, status, error) {
        // executed if something went wrong during call
        if (xhr.status > 0) alert('got error: ' + status); // status 0 - when load is interrupted
    }
});

如果您出於簡單性而不是可用性的考慮,可以刪除錯誤回調部分,並且可以通過引用jquery ajax doc添加更多選項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM