开发者

Calling Linq to SQL method in Web service with jQuery

开发者 https://www.devze.com 2022-12-17 03:47 出处:网络
I have a test web service called: MySimpleService.svc with a method called:GetUserNamesByInitials. below is the Linq to SQL code:

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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号