views:

38

answers:

1

Here's my jquery:

$.ajax({
        type: 'GET',
        url: '/services/Service.asmx/FamilyHistory',
        data: JSON.stringify({
            userID: 10,
            historyID: famid
        }),
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function(val) {
            var famHist = val.d;
            alert(famHist.ID);
        },
        error: function() {
            parent.$.jGrowl('<b>Failed</b>',
                 {
                     header: 'User Action:',
                     life: 3000
                 });
        }
    });

My Class:

public sealed class FamilyHistoryEntity
{
    public string ID { get; set; }
    public string RelativeName { get; set; }
}

My Web service:

    [WebMethod(EnableSession = true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
    public FamilyHistoryEntity FamilyHistory(int userID, string historyID)
    {
         return GetFamilyHistory(historyID, userID); // returns a FamilyHistoryEntity class
    }

Problem is, i can't even make it do a breakpoint onto the webservice, it just throws the jquery ajax event of error.

+1  A: 

Why are you using the stringify function? It would seem like it is looking to call a method with a string parameter rather than the two parameters you have for your method.

Maybe I am missing something?

EDIT: So you would change the data property to:

data: { userID: 10, historyID: famid },

Especially since you specify the contentType as json.

spinon
we need to stringify the json to pass a parameter to asp.net webservice.
Martin Ongtangco
@Martin ok. So are we certain that famid is a string variable and not an int?
spinon
ok the data part worked, it got through the webservice. but when it's returning the value, jquery's throwing a error event. do i need to [Serialize] the C# class?
Martin Ongtangco
What is the error that you are getting in your function? You shouldn't have to serialize the class.
spinon
definitely not coming from my webservice, it's returning correct data. would specific datatypes factor from c# to javascript? like DateTime? (nullables)
Martin Ongtangco
can you try adding the error parameters to the error function and then see what the error message is:error:function (xmlHttpRequest, ajaxOptions, error){ alert(xmlHttpRequest.status); alert(error); }
spinon
I got a "200" on the xmlHttpRequest.status and "undefined" on the error event.
Martin Ongtangco
it also said "parsererror" on the ajaxOptions
Martin Ongtangco
Can you post the response that came back from the server? You should be able to inspect the xhr object to see.
spinon
the xhr.responseText returns a well formed xml. shouldn't it return an array because its json? am i missing something else?
Martin Ongtangco
I fixed it. I gave up on HTTPGEt and used HTTPPOST instead. thanks man
Martin Ongtangco
Glad to hear you got it working.
spinon