2014-09-11 33 views
2

这已经让我的头几天了。我正在创建一个列表网站。房源来自直接上市在我们的网站(使用数据库)创建。此外,我们以数据文件的形式从另一个网站获取列表。如何在同一时间获取并发布方法

首先,有没有人做过同样的工作?我怀疑这是否可能。 我们是否可以从两个来源(直接从数据库中列出数据并从另一个网站获取数据)获取列表,并将它们全部显示在同一页面上(例如页面a)?

如果是,那么我们如何使用一个搜索表单搜索列表结果?我假设get和post方法需要同时使用。因为获取数据馈送,我们下面

<form name="Search_Form" method="get" action="http://a.com/a/a.php" onsubmit="return checkForm()"> 

使用,并张贴了直接上市,从数据库如下

<form method="post" action="a.php"> 

任何建议将高度赞赏的。

感谢满口

回答

3

你不能同时获取和POST,它们是两种不同的HTTP方法。您要么向URL发出GET请求或POST请求。

如果您需要从外部数据源中抓取数据,那么这就是GET。如果用户在您的网站上进行搜索,那么通常也是一个GET(因为如果查询参数在URL中,那么它可以被缓存,被搜索引擎抓取等)。

在您的场景中,服务器端PHP脚本将通过say,cURL对外部数据馈送执行GET请求,并将结果保存到变量中;你也可以在这个脚本中查询你的数据库;然后使用用户提交的值最终过滤结果。

我不确定POST在哪里出现,除非我误解了你的问题。

2

而不是让表单提交您的请求让你的checkForm例程分别使用ajax进行调用?然后,您可以将结果与您正在做的任何展示结合起来。请记住让checkForm返回false;

我注意到你还没有接受答案,而且我的含义很模糊。如果您想要从两个来源收集数据,您可以使用GET获取数据,使用POST获取数据。我使用ajax包含一个JavaScript示例。当你点击你的按钮时,checkForm函数会发送一个POST请求,并在完成时发送一个GET到第二个服务。结果可以结合使用,并将它视为一个操作。这是工作代码(当然,但是,您必须将其调整为适合您的服务)。

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> 
<title>Form</title> 
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" /> 
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script> 

    <script type="text/javascript"> 


    var postData = null; 

    $(document).ready(function() { 

     $("#formDiv").show(); 
     $("#tableDiv").hide(); 

    }); 

    function checkForm() 
    { 

     postData = $("#Search_Form").serialize(); 

     alert(postData); 

     $.ajax({ 
       type: "POST", 
       url: "http://localhost:8080/FirstAjaxJspTest/AjaxService", 
       data: postData, // form's input turned to parms 
       contentType: "text", 
       success: function(data) 
       { 
        tableBuild(data); 
       }, 
       complete: function(data) { 
        nextAjaxCall(); 
       }, 
       failure: function(msg) 
       { 
        alert("Failure: " + msg); 
       } 
     }); 

     return false; 

    } 


    function nextAjaxCall() 
    { 

     $.ajax({ 
       type: "GET", 
       url: "http://localhost:8080/FirstAjaxJspTest/AjaxService", 
       data: postData, // form's input turned to parms 
       contentType: "text", 
       success: function(data) { 
        tableBuild(data); 
        tableDisplay(); 
       }, 
       failure: function(msg) { 
        alert("Failure: " + msg); 
       } 

     }); 

     return false; 

    } 

    function tableBuild(data) 
    { 

     data.forEach(function(entry) { 
      $("#resultsTable").append($("<tr><td>" + entry.name + "</td><td>" + entry.address + "</td></tr>")); 
     }); 
     return; 

    } 

    function tableDisplay() 
    { 

     $("#formDiv").hide(); 
     $("#tableDiv").show(); 
     return; 

    } 

    </script> 

</head> 
<body> 
    <div id="formDiv"> 
     <form id="Search_Form"> 
      Name:<br/> 
      <input type="text" id="name" name="name" /><br/> 
      SSN:<br/> 
      <input type="text" id="ssn" name="ssn" /><br/><br/> 
      <button type="button" onclick="return checkForm()">Submit</button> 
     </form> 
    </div> 
    <div id="tableDiv"> 
     <table id="resultsTable"> 
      <thead> 
       <tr> 
        <th>Name</th> 
        <th>Address</th> 
       </tr> 
      </thead> 
     </table> 
    </div> 
</body> 
</html> 
相关问题