简体   繁体   English

在HTML页面中显示Ajax响应

[英]Display Ajax response in HTML page

I looked over tens of similar questions but I couldn't find the solution I needed. 我查看了数十个类似的问题,但找不到所需的解决方案。 Sorry in advance if this is a duplicate. 抱歉,如果这是重复的。 I want to show the Ajax response in the page, once the window is loaded. 窗口加载后,我想在页面中显示Ajax响应。

Code

HTML HTML

<ul class="rate-ul">
    <li id="LTL">LTL Freight</li>
</ul>

and the jquery part to change it is: 而要更改它的jQuery部分是:

Javascript/jQuery 使用Javascript / jQuery的

if(condition){
$("#LTL").change(UpdateLTLRating);
}
function UpdateLTLRating() {
    console.log("update shipping called");
    $.ajax({
        url: window.location.href,
        data: 'call=ajax&method=ltlRateList&'
        type: 'POST',
        success: function (resp) {
        console.log(resp)
        $(".rate-ul").html(resp);
        },
         error: function (xhr, status) {
            alert("Sorry, there was a problem!");
        }
    });

I get the update shipping called in the console, but I don't get resp called on the windows load. 我在控制台中调用了更新版本,但是在Windows加载中没有调用resp When I attach it to a button click event, I get the resp in the console as well. 当我将其附加到按钮单击事件时,我也会在控制台中得到resp I haven't been able to get resp on the page. 我无法在页面上得到resp

From your sample, it's not clear that you're even calling your function on window load to get the data you want, if this is the case in your original code, here's how you can do it: 从您的示例中,尚不清楚您是否甚至在窗口加载时调用函数以获取所需的数据,如果在原始代码中就是这种情况,则可以按照以下方法进行操作:

if(condition){
    $("#LTL").change(UpdateLTLRating);
}
function UpdateLTLRating() {
    console.log("update shipping called");
    $.ajax({
        url: window.location.href,
        data: 'call=ajax&method=ltlRateList&'
        type: 'POST',
        success: function (resp) {
        console.log(resp)
        $(".rate-ul").html(resp);
        },
         error: function (xhr, status) {
            alert("Sorry, there was a problem!");
        }
    });
}

$(document).ready(function() {
    UpdateLTLRating();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM