2017-08-09 84 views
0

對於奇怪的問題措辭感到抱歉。坦率地說,我在這裏深陷困境,不得不同時學習大量數據。使用AJAX,MVC,JQuery進行異步數據庫搜索

我想弄清楚如何使用AJAX在數據庫上執行異步搜索,但它不斷出現「500內部服務器錯誤」。我做了一些AJAX教程,但沒有一個涉及到web開發的MVC方法。有些信息需要發送到哪裏以及如何發送,我有點失落。

我所知道的是500服務器錯誤意味着它發生在服務器端而不是客戶端,所以我認爲在控制器開始涉入的地方有一個邏輯中斷。這只是一個新手猜測。

我會粘貼我認爲是相關代碼的所有內容。在上下文中,數據庫信息來自我一直在爲實踐工作的模擬銀行數據庫中的「賬戶」表。

感謝您的任何幫助。

首先,這裏的錯誤信息在調試信息在Chrome

Internal Server Error 500

看,當我得到現在,這裏是所涉及的代碼。

的JavaScript/JQuery的:

var $j = jQuery.noConflict(); 

    var key = 0; 

    $j(function() { 

     $j("#search_btn").click(function() { 
      key = $j("#acc-id-search").val(); 
      searchAcc(); 
      return false; 
     })   
    }); 



    function searchAcc() { 

     var callback = function (data) { 

      $j("#account_data_table").empty(); 
      var htmlArray = []; 
      if (data.total > 0) { 
       $j.each(data.items, function (i, item) { 
        htmlArray.push("<tr>"); 
        htmlArray.push('<td class="text-center">' + item.account_id + '</td>'); 
        htmlArray.push('<td class="text-center">' + item.product_id + '</td>'); 
        htmlArray.push('<td class="text-center">' + item.cust_id + '</td>'); 
        htmlArray.push('<td class="text-center">' + item.open_date + '</td>'); 
        htmlArray.push('<td class="text-center">' + item.close_date + '</td>'); 
        htmlArray.push('<td class="text-center">' + item.last_activity_date + '</td>'); 
        htmlArray.push('<td class="text-center">' + item.status + '</td>'); 
        htmlArray.push('<td class="text-center">' + item.open_branch_id + '</td>'); 
        htmlArray.push('<td class="text-center">' + item.open_emp_id + '</td>'); 
        htmlArray.push('<td class="text-center">' + item.avail_balance + '</td>'); 
        htmlArray.push('<td class="text-center">' + item.pending_balance + '</td>'); 
        htmlArray.push("</tr>"); 
       }); 
      } 
      else { 
       htmlArray.push('<tr><td colspan="10">No results!</td></tr>'); 
      } 

      $j("#account_data_table").append(htmlArray.join('')); 
      alert("Sucess?"); 

     }; 
     alert("Searching for '" + key + "'"); 
     postData('@Url.Content("~/Accounts/Index")', key, callback, '#account_data_table'); 
    } 

    function postData(url, data, callback, lockObj, dataType, loadingMessage) 
    { 
     data = data || {}; 
     dataType = dataType || 'json'; 
     loadingMessage = loadingMessage || 'Loading...';; 
     var isLock = !!lockObj; 
     $.ajax({ 
      url: url, 
      data: data, 
      method: 'post', 
      dataType: dataType, 
      cache: false, 
      beforeSend: function(){ 
       alert("About to send"); 
      }, 
      success: callback, 
      error: function(){ 
       alert("Failed..!"); 
      }, 
      complete: function(){ 

      } 
     }); 
    } 

是 '@ Url.Content( 「〜/會計/索引」),' 點到控制器:

[HttpPost] 
    public NewtonJsonResult Index(int key) 
    { 
     var _service = new BankEntities(); 
     var searchCondition = new account() { account_id = key }; 
     var resultObj = new AjaxDataResult(); 
     var allitems = _service.All(searchCondition); 
     var itemArray = new JArray(); 
     resultObj.total = allitems.Count(); 
     JObject temp; 
     foreach(var item in allitems) 
     { 
      temp = new JObject(); 
      temp["account_id"] = item.account_id; 
      temp["product_cd"] = item.product_cd; 
      temp["cust_id"] = item.cust_id; 
      temp["open_date"] = item.open_date; 
      temp["close_date"] = item.close_date; 
      temp["last_activity_date"] = item.last_activity_date; 
      temp["status"] = item.status; 
      temp["open_branch_id"] = item.open_branch_id; 
      temp["open_emp_id"] = item.open_emp_id; 
      temp["avail_balance"] = item.avail_balance; 
      temp["pending_balance"] = item.pending_balance; 
      itemArray.Add(temp); 

     } 
     resultObj.items = itemArray; 
     return new NewtonJsonResult(resultObj); 
    } 

'全部(searchcondition)'使用哪種方法來在表中查找所需的項目:

public List<account> All(account acc) 
    { 
     var data = accounts.Where(x => x.status == "ACTIVE"); 
     if(acc.account_id != 0) 
     { 
      data = data.Where(x => x.account_id == acc.account_id); 
     } 
     return data.OrderBy(x => x.account_id).ToList(); 
    } 
+0

嘗試移除'數據類型:dataType'從AJAX,好像key'不獲取傳遞到控制器所需的PARAM'。 – User3250

+0

檢查你的服務器日誌可能是你的服務器錯誤。 –

+0

@ User3250沒有運氣我很害怕,仍然是同樣的問題。有沒有一種方法可以檢查「關鍵」參數是否正在發送?我無法弄清楚如何在控制器端執行分步調試過程。 –

回答

0

特別感謝@nnnnnn,其答案在評論S中的最初的問題後:

的方法可能不被調用,因爲你沒有設置正確的 請求參數,所以沒有方法存在請求因此500 錯誤。您當前的代碼嘗試包含鍵值,但不給 提供請求參數名稱。嘗試更改密鑰= $ j(「#acc-id-search」)。val(); to key = {key:$ j(「#acc-id-search」)。val() } ;. (實際的請求將隨後結束與一個參數 鍵= thesearchvaluehere。)