views:

620

answers:

2

Hello,

I followed the directions in this post: http://stackoverflow.com/questions/457183/how-can-we-integrate-jquery-autocomplete-using-asp-net-webservie-and-sql-databas

to make jquery-autocomplete plugin work with my webservice on asp.net, but it just won't work.

Autocomplete works if I do local data. I'm also able to make calls to my webservice using jquery ajax. Can anybody tell me what I'm doing wrong? Code:

//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;
        }
    }

EDIT:

This is the response I get titled internal server error.

<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)
-->

EDIT:

So I removed the /HelloWorld/ part from the url, and the response doesn't generate an error anymore. However, this doesn't solve my problem. I'm thinking there's another way to pass in the method name to call?

A: 

Your first example uses datatype json but your second one with autocomplete uses xml. Change both to json?

seth
I changed the second one to json, still the same error.
A: 

OK, so I figured it out.

This is the jquery autocomplete plugin 1.1 by Jörn Zaefferer

First you need to modify the jquery.autocomplete.js file in the following way:

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,

this allows you to specify the type of the request. We want to specify "POST" as the option if we want to call the webmethod of a webservice.

Now when you perform the autocomplete call as I am, specify the following:

type: "POST",
dataType: "xml"

This tells the jquery ajax to do a post to the method. And voila, it works!