2016-06-15 9 views
1

Ich habe ein paar andere Fragen gelesen, kann aber immer noch nicht die Antwort auf meine spezifische Frage finden. Ich versuche, ein vb.net Sub mit einem Ajax Post zu nennen. Die Ajax-Funktion trifft den Erfolg, trifft aber niemals meine Haltepunkte in meinem Sub oder schlägt tatsächlich in meine Datenbank ein. Jede Hilfe wird geschätzt.Ajax Post Hitting Erfolg, aber keine VB.Net-Funktion

aspx.vb

<System.Web.Services.WebMethod()> _ 
Public Shared Sub insertKPI(id As String) 
    Dim constr As String = ConfigurationManager.ConnectionStrings("SqlServerConnectionString").ConnectionString 
    Using con As New SqlConnection(constr) 
     Using cmd As New SqlCommand 
      cmd.CommandText = "kpi_InsertKPI" 
      cmd.CommandType = CommandType.StoredProcedure 
      cmd.Parameters.AddWithValue("PartnerLinkCode", HttpContext.Current.Session.Contents("partnerlinkcode").ToString) 
      cmd.Parameters.AddWithValue("UserLinkCode", HttpContext.Current.Session.Contents("userlinkcode").ToString) 
      cmd.Parameters.AddWithValue("KPIInfoLinkCode", id) 
      cmd.Connection = con 
      con.Open() 
      cmd.ExecuteNonQuery() 
      con.Close() 
     End Using 
    End Using 
End Sub 

Script

function addWidget(id){ 
    $.ajax({ 
     type: "POST", 
     url: "../secure/scorecardtable.aspx/insertKPI", 
     data: '{"id":' + JSON.stringify(id) + '}', 
     datatype: "json", 
     success: function(result){ 
      alert("New kpi should be in database"); 
      console.log(result); 
     }, 
     error: function(xhr, textStatus, errorThrown){ 
      alert("There was an error inserting the kpi into the database. " + xhr.status + ': ' + errorThrown); 
     } 
    }); // End Ajax 
} 
+0

Erstellen Sie '.asmx 'Webdienst oder WCF-Dienst (' .svc'). Webseiten verarbeiten normalerweise keine Ajax-Anfragen. –

Antwort

0

Hinzufügen contentType meiner Ajax-Funktion tatsächlich machte es funktionieren.

function addWidget(id){ 
    $.ajax({ 
     type: "POST", 
     url: "../secure/scorecardtable.aspx/insertKPI", 
     data: '{"id":' + JSON.stringify(id) + '}', 
     contentType: "application/json; charset=utf-8", 
     datatype: "json", 
     success: function(result){ 
      alert("New kpi should be in database"); 
      console.log(result); 
     }, 
     error: function(xhr, textStatus, errorThrown){ 
      alert("There was an error inserting the kpi into the database. " + xhr.status + ': ' + errorThrown); 
     } 
    }); // End Ajax 
}