4

My page contains the following code in the function called when a button is clicked:

$.ajax({   
    type: "POST",   
    url: "ProviderPortal.aspx/ppTransferToProvider",   
    data: "{ICA_Key:'" + ICAKey + "', PROV_Key:'" + Provider + "'}",   
    contentType: "application/json; charset=utf-8",
    async: true,           
    cache: false,    
    dataType: "json",   
    success: function(msg) {     
        alert(msg) 
    } 
}); 

This code is modeled after the selected answer to Calling an ASP.NET server side method via jQuery

However, I'm not sure if the ASP.NET (VB, Net 2.0) function signature

<WebMethod()> _
Public Shared Function ppTransferToProvider( _
    ByVal ICA_Key As String, _ 
    ByVal Prov_Key as String) As String

on the page will receive the data as distinct parameters, or as a string that will have to be parsed for these values. Also, I can't determine if the call is even being made correctly; it seems that a breakpoint in the function itself is never getting hit, so I can't see it for myself.

a) Are parameters passed as just a big string, or are they correctly passed to my function with two parameters? b) How can I trace the execution of the ASP.NET function? Or is it possible to trace, given that it's shared?

Community
  • 1
  • 1
taserian
  • 624
  • 6
  • 17

3 Answers3

6

The problem is you're passing a string while you're service is expecting a set of objects. Try the following instead

data: {ICA_Key: ICAKey, PROV_Key: Provider },
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • Thank you for replying! Do you know if it's possible to add a breakpoint in the shared function and trace its execution? – taserian Nov 03 '11 at 17:12
  • @taserian yes it's possible. It shouldn't be any different than a normal function. Note: I just realized it was a `Shared` method. It may need to be a non-shared method in order to work properly as part of the web service. I don't know for sure though. – JaredPar Nov 03 '11 at 17:29
  • Er, no. The data property must be enclosed in strings. The way this was written in the original question is correct, save for the PROV_Key property. – jwheron Nov 03 '11 at 17:57
3

If you are going to pass objects, bite the bullet and use JSON2.stringify. You can then work with js object and now worry about string manipulation, quotes, parsing..etc:

var exmapleParams = {ICA_Key: ICAKey, PROV_Key: Provider };

$.ajax({   
    type: "POST",   
    url: "ProviderPortal.asmx/ppTransferToProvider",   
    data: JSON2.stringify(exmapleParams),   
    contentType: "application/json; charset=utf-8",
    async: true,           
    cache: false,    
    dataType: "json",   
    success: function(msg) { //ms web services return .d
        alert(msg.d) 
    } 
}); 

<WebMethod()> _
Public Shared Function ppTransferToProvider( _
    ByVal exmapleParams As CustomObject) As String
rick schott
  • 21,012
  • 5
  • 52
  • 81
1

However, I'm not sure if the ASP.NET (VB, Net 2.0) function signature [...] on the page will receive the data as distinct parameters, or as a string that will have to be parsed for these values

You'll get distinct values. Your data property in your $.ajax call may need to be changed from:

data: "{ICA_Key:'" + ICAKey + "', PROV_Key:'" + Provider + "'}",   

to:

data: "{ICA_Key:'" + ICAKey + "', Prov_Key:'" + Provider + "'}",   

I say may only because I don't know if VB.NET is case sensitive in this regard, but C# does require the case of the JavaScript object's property to exactly match the parameter in the AJAX WebMethod.

Note that your VB function expects variables named ICA_Key and Prov_Key. If the name PROV_Key is correct, then you likely need to rename your VB parameter to match what you're passing.

b) How can I trace the execution of the ASP.NET function? Or is it possible to trace, given that it's shared?

You can trace the execution of your WebMethod, but you do need to remove the shared keyword (From my understanding, shared in VB is equivalent to static in C#, so my answer is based off that).

Hope this helps.

jwheron
  • 2,553
  • 2
  • 30
  • 40
  • But the decorator only works with Shared (static) functions, from what I've read. – taserian Nov 03 '11 at 19:23
  • The examples in MSDN don't include that keyword, and none of my WebMethods use the static keyword. I changed them to static to test my `$.ajax` calls, and they stopped working with that keyword. – jwheron Nov 03 '11 at 19:31
  • It's my mistake above. It's not an asmx web service, but an aspx page. Since the page class can be instantiated, the Shared keyword is needed, but that breaks my traceability. – taserian Nov 03 '11 at 19:58
  • Ah, okay. Is there any reason why you need it to be on an aspx page? It's my understanding that, although asmx web services are legacy, there's more support for them than aspx page methods. – jwheron Nov 03 '11 at 20:07
  • There are asmx webservices in use here, but they all require credentials from Active Directory, which users of the Provider Portal won't have. The WebMethod function I'm using impersonates an authorized user to call the required webservice, adding a degree of separation from the web service methodology in use here. – taserian Nov 03 '11 at 20:29
  • Hm, okay -- you certainly know your system better than I do. :) Does my other suggestion (about the case of PROV_Key vs. Prov_Key) get you anywhere? – jwheron Nov 03 '11 at 20:39
  • I changed it around to use Rick Schott's JSON.stringify to pass the parameters. Makes mor much easier unwinding. Still, thank you for sticking around! – taserian Nov 03 '11 at 20:42
  • You're definitely right. I `stringify` all my objects if I have more than two parameters (this is just an arbitrary line I drew in the sand for myself). If you need any sort of validation, you can leverage the stringified class's existence and build in validation functions (which I would **HIGHLY** suggest you do). In any case, glad to help! – jwheron Nov 03 '11 at 20:46