如何通过js将参数发送到ajax js文件

时间:2016-08-17 21:40:46

标签: javascript jquery ajax

我的HTML中有这个:

 <script src="../scripts/jquery-1.8.1.min.js"></script>
 <script src="../scripts/displayTable.js"></script>

displayTable.js由以下内容组成:

$.ajax({
    url: "https://carcraft.atsbusinessandgames.com/xmls/carcraft_1-10-2Test.xml",
    type: "Get",
    dataType: 'xml',
    success: function (result) {
        $(result).find('Module').each(function() {
             var name = $(this).attr("name");
            var url = $(this).find('url').text();
            var authors = $(this).find('authors').text();
            var version = $(this).find('version').text();
            var date = $(this).find('date').text();
            var description = $(this).find('description').text();
            $("#ModsList").append("<tr>" + "<td>" + "<a href=" + url + ">" + name + "</a>" + "</td>" + "<td>" + authors + "</td>" + "<td>" + version + "</td>" + "<td>" + date + "</td>" + "<td>" + description + "</td>" + "</tr>");
        });
    },
    failure: function() {
    alert("Notify the site owner that the xml file is unreadable.");
    }
});

我如何制作它以便我可以将url作为参数传递而不是将其硬编码到js文件中,这样我可以在我的网站上的许多页面中使用相同的文件?

2 个答案:

答案 0 :(得分:1)

displayTable.js中定义一个函数,然后在加载脚本后调用它。

因此diaplayTable.js将包含:

function displayTable(url) {
    $.ajax({
        url: url,
        type: "Get",
        dataType: 'xml',
        success: function (result) {
            $(result).find('Module').each(function() {
                 var name = $(this).attr("name");
                var url = $(this).find('url').text();
                var authors = $(this).find('authors').text();
                var version = $(this).find('version').text();
                var date = $(this).find('date').text();
                var description = $(this).find('description').text();
                $("#ModsList").append("<tr>" + "<td>" + "<a href=" + url + ">" + name + "</a>" + "</td>" + "<td>" + authors + "</td>" + "<td>" + version + "</td>" + "<td>" + date + "</td>" + "<td>" + description + "</td>" + "</tr>");
            });
        },
        failure: function() {
            alert("Notify the site owner that the xml file is unreadable.");
        }
    });
}

然后你用它作为:

<script src="../scripts/jquery-1.8.1.min.js"></script>
<script src="../scripts/displayTable.js"></script>
<script>
$(document).ready(function() {
    displayTable("https://carcraft.atsbusinessandgames.com/xmls/carcraft_1-10-2Test.xml");
});
</script>

答案 1 :(得分:0)

你走了,

function callAjax(url){
    $.ajax({
      url: url,
      type: "Get",
      dataType: 'xml',
      success: function (result) {
        $(result).find('Module').each(function() {
             var name = $(this).attr("name");
            var url = $(this).find('url').text();
            var authors = $(this).find('authors').text();
            var version = $(this).find('version').text();
            var date = $(this).find('date').text();
            var description = $(this).find('description').text();
            $("#ModsList").append("<tr>" + "<td>" + "<a href=" + url + ">" + name + "</a>" + "</td>" + "<td>" + authors + "</td>" + "<td>" + version + "</td>" + "<td>" + date + "</td>" + "<td>" + description + "</td>" + "</tr>");
        });
      },
      failure: function() {
        alert("Notify the site owner that the xml file is unreadable.");
      }
  });
};

调用该函数并将url作为参数传递给你想要调用的地方。

callAjax("https://carcraft.atsbusinessandgames.com/xmls/carcraft_1-10-2Test.xml"); 
callAjax("https://google.com");