views:

318

answers:

1

Hello,

I have a test web service called: MySimpleService.svc with a method called:GetUserNamesByInitials. below is the Linq to SQL code:

[OperationContract]   
public static string GetUserNamesByInitials(string initials)
{
        string result = "";

        //obtain the data source
        var testDB = new TestDBDataContext();

        //create the query
        var query = from c in testDB.TestTables
                    where c.initials == initials
                    select c;

        //execute the query
        foreach (var c in query)
        {
            result = result + c.full_name;
        }

        return result;
}

I have used that code in page behind and it works well. However when i try to call it using jQuery I don't get any result, no errors nothing. below is the code i use in jQuery:

$('#TextBox3').live('keydown', function(e) {
    var keyCode = e.keyCode || e.which;

    if (keyCode == 9) {
        e.preventDefault();

        //call function here
        $.ajax({

            type: "POST",
            url: "/MySimpleService.svc/GetUserNamesByInitials",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: '{"word":"' + $('#TextBox3').val() + '"}',
            success: function(data) {

                $('#TextBox4').val(data.d);


            }

        });


    }


});

});

what I do is to type the user id in one textbox (TextBox3) and when I press the Tab key the result is shown in another textbox (TextBox4). The jQuery call works well with other methods that do not call the database, for example using this other web service method it works:

[OperationContract]
public string ParameterizedConnectionTest(string word)
{
    return string.Format("You entered the word: {0}", word);    
}

However with the Linq method it just does not work. Maybe i am doing something wrong? thank you in advance.

+1  A: 

The web service is expecting a string named "initials" and you're sending it a string called "word". More than likely it is throwing an exception that it's not being passed the expected parameter.

Try adding an error: function() in your $.ajax() call to catch the error. I would also suggest installing FireBug or Fiddler to be able to view and debug the AJAX request and response. Here's the updated $.ajax() call with the right parameter name and an error function which should invoke your chosen javascript debugger if you have it open:

$.ajax({
 type: "POST",
 url: "/MySimpleService.svc/GetUserNamesByInitials",
 contentType: "application/json; charset=utf-8",
 dataType: "json",
 data: '{"initials":"' + $('#TextBox3').val() + '"}',
 success: function(data) {
  $('#TextBox4').val(data.d);
 },
 error: function(XMLHttpRequest, textStatus, errorThrown) {
  alert('Error!');
  debugger; // Will let you see the response in FireBug, IE8 debugger, etc.
 }
});

Update

So I just noticed that you're using WCF services instead of normal ASP.Net (ASMX) services... Some additional suggestions:

  1. Enable IncludeExceptionDetailInFaults in your web.config so that you can debug any errors happening. ASP.Net services will return an error by default but you need to explicitly turn this on for WCF.

  2. Try adding an additional attribute below your [OperationContract] attribute to specify you're expecting a post with JSON data:

    [WebInvoke(Method = "POST",BodyStyle=WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
    
Lance McNearney
thanks for the correction. Yes i have corrected the jQuery string to display "initials" but i still get no response. and when I add the error function to jQuery code it gives me syntax error :(
Tika
Tika: The code was missing a comma. I updated it. Once you know what error is being returned we can be more helpful to you.
Lance McNearney
Thanks for helping me solve this issue,The debugger works well now, In the XMLHttpRequest node of the scrip tab in firebug I get some error messages:get responseXML Document childNodes [XMLStylesheetProcessingInstruction { baseURI="http://localhost/MySimp.../GetUserNamesByInitials", more...}, parsererror] 0 XML Parsing Error: no element found Location: http://localhost/MySimpleService.svc/GetUserNamesByInitials Line Number 1, Column 1: get status 404get status text "Not Found"errorThrown undefinedtextStatus "error"
Tika
So you're getting a 404 error which means that the web service being called isn't setup on the server (it's returning "not found" instead of throwing a 500-type exception/error). Can you browse to /MySimpleService.svc and do you get a page with information on the services available?
Lance McNearney
Thanks, When i try to browse to it the page shows the message:This is a Windows© Communication Foundation service.Metadata publishing for this service is currently disabled.with instructions to change the behavior in the web config file.However i can still use the web service whenever I invoke the ParameterizedConnectionTest method. I am now learning the WCF services and I am not sure if they can be browsed just like the other webservices are.What I do is create a Website and then add the Ajax-enabled WCF Service. I feel we are getting close to solving this issue.thank you.
Tika