0

我正在寻找一种方法来将 GET Form 结果页面定位到 div。到目前为止,大多数解决方案都告诉我改用 Iframe,但我的目的是对结果页面进行一些更改,并且使用 Iframe 似乎会激活跨域脚本规则,从而阻止我获取页面内容。

结果页面通常是原始 XML。

<html>
<head>
<link rel="stylesheet" href="css/smoothness/jquery-ui-1.9.2.custom.css" >
<script src="js/jquery-1.8.3.js"></script>
<script src="js/jquery-ui-1.9.2.custom.js"></script>
<script src="js/jquery.xslt.js"></script>
<script type="text/javascript">

$(function() {
    var xmlContent = $("#CFrame").contents().find("*").text();

    $('#SResult').xslt({xml:xmlContent, xslUrl:'stylesheet/styleSheet.xsl'});
});

// prepare the form when the DOM is ready 
function submitForm() { 
    // bind form using ajaxForm 
    $('#searchForm').ajaxForm({ 
        // dataType identifies the expected content type of the server response 
        dataType:  'xml', 
        // success identifies the function to invoke when the server response 
        // has been received 
        success:   processXml
    });
    return false; 
}

function processXml(responseXML) { 
    // 'responseXML' is the XML document returned by the server; we use 
    // jQuery to extract the content of the message node from the XML doc 
    var message = $(responseXML).text(); 
    $("#SResult").text(message); 
}
</head>
<body>
   <form id="searchForm" method="GET" action="http://111.11.111.11:1111/search" onSubmit="submitForm()">
     <!--.... Some input go here-->
    <input type="submit" value="Search" />
<div id="SResult">
</div>
<iframe id="CFrame" name="ContentFrame" frameborder="1" height="1000px" width="1000px" scrolling="no"></iframe>
</body>
<script>
loadUI();
</script>
</html> 

谢谢,

4

3 回答 3

0

您是否使用 get 方法提交表单,然后响应将显示在 div 标签上?

你可以使用 Jquery ajax。

例子

$.ajax({
   type: "POST",
   url: "url here",
   data: {id: someIdTobePass},
   success: function(response){
      //do the jquery manipulation
      $("#myDivID").html("the response is " + response);
   }
});
于 2013-01-07T03:15:09.580 回答
0

您可以在 jQuery 中使用$.get辅助方法。

jQuery API

jQuery.get( url [, data ] [, success(data, textStatus, jqXHR) ] [, dataType ] );

代码示例

$.get("test.php", { name: "John", time: "2pm" },function(data){
   $("#mydiv").html(data);
});
于 2013-01-07T04:54:26.823 回答
0

除非您使用 jsonp(服务器代理)或者您可以控制其他域,否则您无法向其他域发出 get 或 post 请求。如果您可以控制要进行 ajax 调用的域,则可以将Access-Control-Allow-Origin响应标头添加到您尝试请求的页面,这将允许您的域访问。

由于您正在执行返回 XML 的 GET 请求,因此我建议使用这个使用 YQL 作为代理的jQuery 插件。此外,您可以使用本指南来查看它是如何工作的,或者在没有插件的情况下进行操作。

解决了同样问题的 stackoverflow 问题:Cross-Domain Requests with jQuery

于 2013-01-07T05:35:43.170 回答