简体   繁体   中英

Calling ASP.NET function using JQuery $.AJAX call

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?

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 },

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM