2009-09-02 55 views
0

我跟着這個職位的方向:how can we integrate jquery autocomplete using asp.net, webservie and sql database我不能讓與AJAX調用自動完成工作,我的web服務

,使jQuery的自動完成插件的工作與我的asp.net web服務,但它只是不會工作。

自動完成工作,如果我做本地數據。我也可以使用jquery ajax調用我的web服務。有人可以告訴我我做錯了什麼嗎?代碼:

//This jquery ajax call works perfectly 

function AjaxTest() { 
    $.ajax({ 
     type: "POST", 
     url: "/Services/OSServices.asmx/HelloWorld", 
     data: "{}", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: RenderTest 
    }); 
    } 


//this autocomplete ajax call doesn't work. It doesn't even hit the breakpoint in the 
web service 



$("#lastName").autocomplete("/Services/OSServices.asmx/HelloWorld", { 
       parse: function(data) { 
        var parsed = []; 
        $(data).find("string").each(function() { 
         parsed[parsed.length] = { 
          data: [$(this).text()], 
          value: $(this).text(), 
          result: [$(this).text()] 
         }; 
        }); 
        return parsed; 
       }, 
       datatype: "xml" 
      }); 


//this is the webservice code: 

[WebService(Namespace = "http://tempuri.org")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.ComponentModel.ToolboxItem(false)] 
    [System.Web.Script.Services.ScriptService] 
    public class OSServices : System.Web.Services.WebService 
    { 
     [WebMethod] 
     public string[] HelloWorld() 
     { 
      string[] myArray = { "blah1", "blah2", "blah3" }; 

      return myArray; 
     } 
    } 

編輯:

這是我得到題爲內部服務器錯誤的響應。

<html> 
    <head> 
     <title>Request format is unrecognized for URL unexpectedly ending in '/HelloWorld'.</title> 

     <style> 
     body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} 
     p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px} 
     b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px} 
     H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red } 
     H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon } 
     pre {font-family:"Lucida Console";font-size: .9em} 
     .marker {font-weight: bold; color: black;text-decoration: none;} 
     .version {color: gray;} 
     .error {margin-bottom: 10px;} 
     .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; } 
     </style> 
    </head> 

    <body bgcolor="white"> 

      <span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1> 

      <h2> <i>Request format is unrecognized for URL unexpectedly ending in '/HelloWorld'.</i> </h2></span> 

      <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif "> 

      <b> Description: </b>An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

      <br><br> 

      <b> Exception Details: </b>System.InvalidOperationException: Request format is unrecognized for URL unexpectedly ending in '/HelloWorld'.<br><br> 

      <b>Source Error:</b> <br><br> 

      <table width=100% bgcolor="#ffffcc"> 
       <tr> 
        <td> 
         <code> 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.</code> 

        </td> 
       </tr> 
      </table> 

      <br> 

      <b>Stack Trace:</b> <br><br> 

      <table width=100% bgcolor="#ffffcc"> 
       <tr> 
        <td> 
         <code><pre> 

[InvalidOperationException: Request format is unrecognized for URL unexpectedly ending in '/HelloWorld'.] 
    System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response) +405961 
    System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler(HttpContext context, String verb, String url, String filePath) +212 
    System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated) +47 
    System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig) +193 
    System.Web.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +93 
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +155 
</pre></code> 

        </td> 
       </tr> 
      </table> 

      <br> 

      <hr width=100% size=1 color=silver> 

      <b>Version Information:</b>&nbsp;Microsoft .NET Framework Version:2.0.50727.3082; ASP.NET Version:2.0.50727.3082 

      </font> 

    </body> 
</html> 
<!-- 
[InvalidOperationException]: Request format is unrecognized for URL unexpectedly ending in '/HelloWorld'. 
    at System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response) 
    at System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler(HttpContext context, String verb, String url, String filePath) 
    at System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated) 
    at System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig) 
    at System.Web.HttpApplication.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 
    at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) 
--> 

編輯:

所以我刪除了/的HelloWorld /部分來自URL,響應已不產生 錯誤。但是,這並不能解決我的問題。我想有另一種 的方式來傳遞方法名稱來調用?

+0

好吧,我們需要更多的信息,如顯示正在發出的請求和任何響應以及狀態碼和響應內容的Fiddler或Firebug屏幕截圖。 – redsquare 2009-09-02 17:13:02

+0

我已將響應與錯誤一起添加。謝謝。 – Riz 2009-09-02 17:44:43

回答

0

好的,所以我想通了。

這是jQuery的自動完成由JörnZaefferer

第一個插件1.1,你需要修改jquery.autocomplete.js文件以下列方式:

Find the function $.fn.extend({ 
under the function, find the line: url: isUrl ? urlOrData : null, 
under that line, insert the following line: type: isUrl ? null : urlOrData, 

Then find the function request(term, success, failure) { 
under it, find the line: port: "autocomplete" + input.name, 
under the line, add the following line: type: options.type, 

這允許您指定請求的類型。我們想要指定「POST」作爲選項,如果我們想調用web服務的webmethod。

現在,當您執行的自動完成通話,因爲我,指定以下內容:

type: "POST", 
dataType: "xml" 

這告訴了jQuery AJAX做一個帖子的方法。瞧,它的工作原理!

0

您的第一個示例使用數據類型json,但第二個示例使用xml自動完成。更改兩個json?

+0

我改變了第二個到json,仍然是一樣的錯誤。 – Riz 2009-09-02 18:15:03