2016-03-17 42 views
0

我正嘗試在我的文本框中使用來自Web方法的數據實現自動完成。 WebMethod的行爲像瀏覽器:使用Ajax返回自動完成沒有找到與請求URI相匹配的HTTP資源

http://localhost/psa/DesktopModules/PsaMain/API/ModuleTask/GetIssuers?SearchString=e 

,這些都是結果:

[{"IssuerID":1,"Name":"test tester","ChequeAccountNumber":"12345678","CurrencyCode":"EUR"}] 

現在,我想在文本框從響應添加的名稱的數據,但我得到的錯誤在下面的函數:

{"Message":"No HTTP resource was found that matches the request URI 'http://localhost/psa/DesktopModules/PsaMain/API/ModuleTask/GetIssuers?{ 'SearchString': 'e'}'.","MessageDetail":"No action was found on the controller 'ModuleTask' that matches the request."} 

下面,我的AJAX調用似乎失敗的原因,我相信來自錯誤地傳遞參數。我目前還沒有使用ajax的經驗,所以你的意見會很棒。

<script type="text/javascript"> 
$(function() { 
     $("[id$=TextBox1]").autocomplete({ 
      source: function (request, response) { 
       $.ajax({ 
        url: '<%=ResolveUrl("http://localhost/psa/DesktopModules/PsaMain/API/ModuleTask/GetIssuers")%>', 
        data: "{ 'SearchString': '" + request.term + "'}", 
        dataType: "json", 
        type: "GET", 
        contentType: "application/json; charset=utf-8", 
        success: function (data) { 
         response($.map(data.d, function (item) { 
          return { 
           label: item.split('-')[0], 
           val: item.split('-')[1] 
          } 
         })) 
        }, 
        error: function (response) { 
         alert(response.responseText); 
        }, 
        failure: function (response) { 
         alert(response.responseText); 
        } 
       }); 
      }, 
      select: function (e, i) { 
       $("[id$=hfIssuerID]").val(i.item.val); 
      }, 
      minLength: 1 
     }); 
    }); 
</script> 

我的Web方法:

public class ModuleTaskController : DnnApiController 
{ 
[AllowAnonymous()] 
[HttpGet()] 
public HttpResponseMessage GetIssuers(string SearchString) 
{ 
    try { 
     List<Issuers> listIssuers = new List<Issuers>(); 
     IssuersController ic = new IssuersController(); 
     listIssuers = ic.GetIssuers(10, SearchString); 
     return Request.CreateResponse(HttpStatusCode.OK, listIssuers.ToJson); 

    } catch (Exception exc) { 
     return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exc); 
    } 
} 
} 

任何想法?

編輯:

$(function() { 
     $("[id$=TextBox1]").autocomplete({ 
      source: function (request, response) { 
       var qstring = '?' + jQuery.param({ 'SearchString': request.term }); 
       $.ajax({ 
        url: '<%=ResolveUrl("http://localhost/psa/DesktopModules/PsaMain/API/ModuleTask/GetIssuers")%>' + qstring, 
        type: "GET", 
        contentType: "application/json; charset=utf-8", 
        success: function (data) { 
         response($.map(data.d, function (item) { 
          return { 
           label: item.split('-')[0], 
           val: item.split('-')[1] 
          } 
         })) 
        }, 
        error: function (response) { 
         alert(response.responseText); 
        }, 
        failure: function (response) { 
         alert(response.responseText); 
        } 
       }); 
      }, 
      select: function (e, i) { 
       $("[id$=hfIssuerID]").val(i.item.val); 
      }, 
      minLength: 1 
     }); 
    }); 

回答

1

您的Web方法是GET並接受querystring但你Ajax調用試圖通過一個JSON對象,這是越來越添加到您的網址的結尾。您可以通過閱讀URL究竟是什麼來看到錯誤信息

您可以做很多不同的事情。這只是一個。

// turn your search object into querystring 
var qstring = '?'+ jQuery.param({ 'SearchString': request.term}); 
// append this querystring to the end of your url 
$.ajax({ 
     url: '<%=ResolveUrl("http://localhost/psa/DesktopModules/PsaMain/API/ModuleTask/GetIssuers")%>' + qstring , 
     // remove data and datatype 
     type: "GET", 
     //... etc 

也在這種情況下,它看起來不像你需要ResolveUrl。

也許嘗試你的網址爲:

url:'http://localhost/psa/DesktopModules/PsaMain/API/ModuleTask/GetIssuers' + qstring;

+0

你的答案修復我原來的問題。然而,現在這個過程並沒有進入「成功」的一部分,而是失敗。當我調試(初學者的調試級別)時,進程轉到ajax源代碼並且永遠不會回來。有任何想法嗎? – alwaysVBNET

+0

檢查您的瀏覽器的檢查員。 E.G如果它是鉻,請按F12 - 查看是否有任何JS錯誤。編輯(或我的代碼)很可能會有一個小的語法錯誤。 – Darren

+0

我迷路了...想通過teamviewer連接嗎? – alwaysVBNET

相關問題