mardi 18 août 2020

Calling web method with parameter using javascript/jquery

Good day Please help me to solve this issue. What i want to do is call a webmethod that accepts string parameter in my jquery. But i can't make it work.

I've been searching and testing codes but it doesn't work when i pass parameter.

Using this code works. But this only calls the web method without parameter

<WebMethod>
    Public Shared Function GetData() As String

        Return DateTime.Now.ToString()
    End Function

And this is the javascript code

<script type="text/javascript">
        
        function CheckDouble() {
            try {
                alert("clicked")
                $.ajax({  
                      type: "POST",  
                      url: "IssueTransSummary.aspx/GetData",  
                      data: '',  
                      contentType: "application/json; charset=utf-8",  
                      dataType: "json",  
                      success: function (response) {  
                          alert(response.d);  
                      },  
                  });  
            }
            catch(err) {
              alert(err.message);
            }
        }
    </script>

Using the code above works perfectly. But what i need is to pass data to the webmethod

so i updated my web method into this

<WebMethod>
    Public Shared Function GetData(ByRef title As String) As String

        Return DateTime.Now.ToString()
    End Function

And the javascript code to this

<script type="text/javascript">
        
        function CheckDouble() {
            try {
                alert("clicked")
                $.ajax({  
                      type: "POST",  
                      url: "IssueTransSummary.aspx/GetData",  
                      data: JSON.stringify({ title: 'MP3'}),  
                      contentType: "application/json; charset=utf-8",  
                      dataType: "json",  
                      success: function (response) {  
                          alert(response.d);  
                      },  
                  });  
            }
            catch(err) {
              alert(err.message);
            }
        }
    </script>

I'm receiving the error 500 internal server errror

I've tried the solutions i found online but it doesn't work for me.

I even add this code but still doesn't work

<ScriptMethod(UseHttpGet:=True, ResponseFormat:=ResponseFormat.Json)>

Please help me. Thank you




Aucun commentaire:

Enregistrer un commentaire