2017-01-30 2 views
-2

Ich weiß, mehrere Abfragen wie unten in diesem Forum existieren, aber ich konnte nicht gefunden, was genau falsch ist in meinem Fall. jQuery Ajax nicht aufrufen C# Webmethod

$.ajax({ 
    type: "POST", 
    url: "default.aspx/GetMaturityValues", 
    data: jsonParams, 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function (response) { 
     alert(response); 
    }, 
    failure: function (response) { 
     alert(response.d); 
    } 
}); 
[System.Web.Services.WebMethod] 
public static string GetMaturityValues(string countryIDList, string xAxis, string yAxis, string bubbleSize) 
{ 
    //some code 
} 

Die Ausführung fließt, wird nicht in C# Code.

Die jsonParams:

var paramList = ''; 
var countryIDList = '1,2,3,5'; 
var xAxis = '1'; 
var yAxis = '2'; 
var bubbleSize = '6'; 
paramList += 'countryIDList' + '":"' + countryIDList; 
paramList += 'xAxis' + '":"' + xAxis; 
paramList += 'yAxis' + '":"' + yAxis; 
paramList += 'bubbleSize' + '":"' + countryIDList; 
paramList = '{' + paramList + '}'; 
var jsonParams = JSON.stringify(paramList); 
+0

* Fließt nicht in C# -Code * Was genau meinst du damit? Möchten Sie den Ajax in C# übersetzen? – RandomStranger

+0

Der Ajax-Code ruft die beabsichtigte Methode nicht auf. – Rohit

+0

Was bekommen Sie bei der Fehlerreaktion? – A3006

Antwort

2

Ich denke, das Problem ist einfach, dass Ihre Parameter beim Post nicht korrekt formatiert sind. Versuchen Sie, diese einfache Abhilfe:

<script type="text/javascript"> 
    var params = {}; 
    params.countryIDList = "test"; 
    params.xAxis = "x"; 
    params.yAxis = "y"; 
    params.bubbleSize = "y"; 

    $.ajax({ 
     type: "POST", 
     url: "Test1.aspx/GetMaturityValues", 
     data: JSON.stringify(params), 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (response) { 
      alert(response); 
     }, 
     failure: function (response) { 
      alert(response.d); 
     } 
    }); 
</script> 

@DarinDimitrov ist (natürlich) richtig, dass Sie davon ab, eine Modellklasse profitieren könnten ... Aber man kann es mit nur stechen Parameter als auch zu arbeiten.

4

Versuchen Sie, ein Modell:

public class MyModel 
{ 
    public IList<int> CountryIDList { get; set; } 
    public int XAxis { get; set; } 
    public int YAxis { get; set; } 
    public int BubbleSize { get; set; } 
} 

, die Ihren WebMethod als Parameter nehmen:

[System.Web.Services.WebMethod] 
public static string GetMaturityValues(MyModel model) 
{ 
    //some code 
} 

und schließlich auf dem Client:

var paramList = { 
    countryIDList: [1, 2, 3, 5], 
    xAxis: 1, 
    yAxis: 2, 
    bubbleSize: 6 
}; 

var jsonParams = JSON.stringify({ model: paramList}); 

$.ajax({ 
    type: "POST", 
    url: "default.aspx/GetMaturityValues", 
    data: jsonParams, 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function (response) { 
     alert(response); 
    }, 
    failure: function (response) { 
     alert(response.d); 
    } 
}); 

In diesem Beispiel werden alle Parameter ganze Zahlen sind, kaufen Sie natürlich auch andere Datentypen wie Strings zum Beispiel verwenden:

public class MyModel 
{ 
    ... 
    public string Foo { get; set; } 
} 

die Pässe als Zeichenfolge vom Client sein kann demnach:

var paramList = { 
    ... 
    foo: 'bar' 
}; 
+0

Muss sein 'var jsonParams = JSON. stringify ({"model": paramList}); 'damit die Parameterbindung funktioniert, wenn' WebMethod' verwendet wird – user1429080

+0

Sie haben Recht, ich werde meine Antwort aktualisieren, um Ihre Bemerkung zu reflektieren. –

Verwandte Themen