Calling an ASP.NET Function Using a JQuery $ .AJAX Call

My page contains the following code in the function called when the 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 response to the Method Invocation on the ASP.NET server side via jQuery

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

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

the page will receive data in the form of individual parameters or as a string that should be analyzed for these values. In addition, I cannot determine if the call is being executed correctly; it seems like the breakpoint in the function itself never hits, so I don't see it for myself.

a) Are the parameters passed only as a large string, or are they correctly passed to my function with two parameters? b) How can I track the execution of an ASP.NET function? Or can one be traced, given what he shares?

+4
source share
3 answers

The problem is that you are passing string while you are a service waiting for a set of objects. Try instead

 data: {ICA_Key: ICAKey, PROV_Key: Provider }, 
+6
source

If you are going to transfer objects, bite the bullet and use JSON2.stringify . Then you can work with the js object and now worry about string manipulations, quotation marks, 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 
+3
source

However, I am not sure whether the signature signature of ASP.NET (VB, Net 2.0) [...] on the page will receive data as separate parameters or as a string that should be analyzed for these values

You will get different values. The data property in your $.ajax call may need to be changed:

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

in

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

I say maybe only because I don’t know if VB.NET is case sensitive in this regard, but C # really requires the property of the JavaScript object to exactly match the parameter in AJAX WebMethod.

Note that your VB function expects variables named ICA_Key and Prov_Key . If the Prov_Key name Prov_Key correct, you probably need to rename your VB parameter according to what you are passing.

b) How can I track the execution of an ASP.NET function? Or can one be traced, given what he shares?

You can track the progress of your WebMethod, but you need to remove the shared keyword (from my understanding, shared in VB is equivalent to static in C #, so my answer is based on that).

Hope this helps.

+1
source

Source: https://habr.com/ru/post/1379514/


All Articles