Я пытаюсь подсчитать сумму двух чисел.
Страница содержит 3 текстовых поля, и на ней жестко закодирован текст
как
:
Код: Выделить всё
Код: Выделить всё
$(document).ready(function () {
debugger;
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "C:/Users/hp/Documents/visual studio 2010/Projects/WebApplication3/WebApplication3/WebService1.asmx/sum",
data: "{'a':'" + $('input[id$=Text1]').val() + "','b':'" + $('input[id$=Text2]').val() + "'}",
dataType: "json",
success: function (data) {
alert(data);
var results = eval('(' + data.d + ')');
if (results == "success") {
$('input[id$=Text3]').val(data);
//$('span[id$=lblErr]').hide();
}
else {
$('span[id$=lblmsg]').hide();
// $('span[id$=lblErr]').show();
}
},
error: function () {
alert('Error');
}
});
});
Код: Выделить всё
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public string sum(string a, string b)
{
string json = "";
int sum = Convert.ToInt32(a) + Convert.ToInt32(b);
System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
json = oSerializer.Serialize(sum);
return json;
}
}
Подробнее здесь: https://stackoverflow.com/questions/179 ... atype-json
Мобильная версия