使用ajax将参数化URL和加载页面加载到div的正确方法是什么

时间:2016-11-22 09:53:33

标签: jquery ajax

我想将页面加载到<div>,但我的想法已经不多了。 根据我的尝试获得所有类型的错误(主要是 阻止跨源请求 ),所以,显然这不是方法。

这个$("#cmbsHost option:selected").text()包含IP,让我们说:139.131.4.5

我的最后一次尝试:

$.ajax({
    type: 'POST',
    url: "http://ip-api.com/#" + $("#cmbsHost  option:selected").text(),
    crossDomain: true,
    //dataType: "jsonp",
    dataType: 'html',
    cache: false,
    success: function (data) {
        console.log(data);
    }

当然还有这个:

$("#dlgWhois").load("http://ip-api.com/#" + $("#cmbsHost  option:selected").text());

那么......怎么做才能做到?

2 个答案:

答案 0 :(得分:0)

您需要加载整页,然后在data内找到元素/文字。

$.ajax({
    type: 'POST',
    url: "http://ip-api.com/#"
    crossDomain: true,
    //dataType: "jsonp",
    dataType: 'html',
    cache: false,
    success: function (data) {
        console.log( $(data).find("#cmbsHost option:selected").text() );
    }
});

答案 1 :(得分:0)

使用正确的查询字符串参数

确保网址正确

&#13;
&#13;
 $.ajax({
                url: "http://ip-api.com?ip=" + $("#cmbsHost  option:selected").text(),
                dataType: 'html',
                type: 'GET',
                success: function (data)
                {                    
                    $("#dlgWhois").html(data);
                }
            })
&#13;
&#13;
&#13;