2014-09-11 31 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> 
相關問題