簡體   English   中英

如何將Ajax調用包含為外部文件?

[英]How do I include an Ajax call as an external file?

我有一個簡單的Ajax調用,當我將它包裝在HTML文件中的結束體標記之前的腳本中的文檔就緒函數中時,它可以正常工作。 但是,當我嘗試將調用移動到外部.js文件(不包括腳本標記和文檔就緒函數)時,調用不起作用。 我嘗試在HTML文件的頭部和正文中添加外部文件,但沒有成功:

<script src="includes_js/login3.js" type="text/javascript"></script>

我在這里沒有包含太多代碼,因為我不確定什么可能有用。

However, when I try to move the call into an external .js file, excluding script tags and the document ready function

嘗試將文檔就緒函數添加到外部java腳本文件中。

$(document).ready(function(){
 alert('worked');
 // ajax call here
});

如果警報運行,則意味着正在加載外部Java腳本文件。 否則, <script>標記中的路徑可能出錯。

您可以通過以下方式從外部文件調用js方法。

 $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: encodeURI("../CtrlName/MethodName"), // adjust your path
        async: true,
        data: JSON.stringify({ "param": _param }),
        dataType: "json",
        success: function (data) {
            console.log(data);
        },
        error: function (data) {
            console.log(data);
        }
    });

暫無
暫無

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

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