Software Archive
Read-only legacy content

Consuming ASMX web service using ajax call

vaibhav_H_
Beginner
1,029 Views

Hello,

Greetings of the day,

I'm using ASMX web service for saving data on to the server  using ajax call, but the call does not return me anything even it does not alert me sucess of failure for HTTP request, Is there any way which will definitely tell me what's the problem between the call,  need help in calling ASMX web service in intel XDK or should I directely make a ajax  call like we do from a HTML Page .  Hope you guys understand the issue and revert back as soon as possible.

 

 

Thanks and regards 

Vaibhav Hedaoo

0 Kudos
3 Replies
Nick_F_2
New Contributor III
1,029 Views

Here is a simple example, I have concatenated the data to send to the server and the ASMX webservice splits it, but these values could be sent as individual values. A True or False is returned to the Javascript function dependant on whether the write was successful or not.

ASMX Webservice code:-
 

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Data.MySqlClient
Imports System.Data
Imports System.Text
Imports System
Imports System.Web.Script.Services
Imports System.Web.Script.Serialization
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:=<a data-cke-saved-href="http://myURL" href="http://myURL">http://myURL</a>)> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class Service
    Inherits System.Web.Services.WebService
    Dim encodedpassword As String
    Dim SQLServerStr As String = "xxx.xxx.xxx.xxx"
    Dim SQLUserStr As String = "Username"
    Dim SQLPassStr As String = "Password"
    Dim myConnString As String = String.Format("server={0};user id={1};password={2}; pooling=false", SQLServerStr, SQLUserStr, SQLPassStr)
    Dim myconn As New MySqlConnection(myConnString)
    Dim sql As String
    Dim mycommand As New MySqlCommand
    Dim ds As New DataSet
    Dim da As MySqlDataAdapter
    <WebMethod()> _
    Public Function saveData(ByVal data As String) As String
        'Try
        Dim myData() As String = Split(data, ",")
        Dim Name As String = myData(0)
        Dim Age As Date = myData(1)
        mycommand.Connection = myconn
        If myconn.State <> ConnectionState.Open Then
            myconn.Open()
        End If
        sql = "INSERT INTO mytable(name,age) VALUES("
        sql = sql & "'" & Name & "',"
        sql = sql & Age & "); SELECT LAST_INSERT_ID()"
        mycommand.CommandText = sql
        Dim RowAdded As Integer = mycommand.ExecuteScalar()
        myconn.Close()
        If RowAdded > 0 Then
            myconn.Close()
            Return True
        Else
            myconn.Close()
            Return False
        End If
        'Catch ex As Exception
        '    myconn.Close()
        '    Return False
        'End Try
    End Function

Here is the XDK Javascript element that calls the ASMX to write to your database.

function saveData(){    
var dataStr = $("#name").val()+","+$("#age").val();
  $.ajax({
    type: "POST",
    url: "http://myURL/service1.asmx/saveData",
        data: "{'data':'" + dataStr + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,
        crossDomain: true,
        success: function (msg) {
            if (msg.d) {
                    navigator.notification.alert("Data Saved",afterSaveData,"Success"); 
                }
                 else 
                {
                    navigator.notification.alert("Unable to save data.",Dismissed,"Error"); 
                }
        },         
        error: function(msg) {
        navigator.notification.alert("Unable to save data:" + msg.d);
        }
 });
return false;
}

 

0 Kudos
vaibhav_H_
Beginner
1,029 Views

Hello Nick,

Thanks a lot for helping me 

Great work done by you. Before Posting question I have tried a lot and also tried the code which you posted but code is still not working for me. also I have make proper domain whitelisting, but still I'm not getting anything in the ajax call response. I don't know what's going wrong  with my code.  

 

0 Kudos
Nick_F_2
New Contributor III
1,029 Views

Hi

I won't know without looking at your code.
Can you zip up the ASMX file and XDK project and send me the files via a private message.

I'll take a look for you.

0 Kudos
Reply