问题描述
注意:我确实搜索了论坛,但没有找到与我的问题相符或有效的任何内容。
大家好,
我正在尝试使用 json 和 ajax 从 asp.net 网络表单对代码隐藏页面中的方法进行简单调用,但它返回的是该页面的 html 而不是该方法的值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
namespace Tags
{
public partial class AJAX3 : System.Web.UI.Page
{
protected void Page_Load(object sender,EventArgs e)
{
}
[WebMethod]
public static string CalculateSum(Int32 Num1,Int32 Num2)
{
Int32 Result = Num1 + Num2;
return Result.ToString();
}
}
}
这是我的网络表单:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AJAX3.aspx.cs" Inherits="Tags.AJAX3" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Call asp.net server side page methods using jQuery AJAX & json</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#btncalculate").click(function () {
//Get values from textBoxes that will be passed to function
var num1 = $('#txtFirstNumber').val();
var num2 = $('#txtSecondNumber').val();
$.ajax({
type: "POST",dataType: "text",contentType: "application/json; charset=utf-8",//Url is the path of our web method (Page name/function name)
url: "AJAX3/CalculateSum",//Pass values to parameters. If function have no parameters,then we need to use data: "{ }",data: "{'Num1':'" + num1 + "','Num2':'" + num2 + "'}",//called on ajax call success
success: function (result) {
$('#dvMsg').text("Sum of " + num1 + " and " + num2 + " = " + result);
},//called on ajax call failure
error: function (xhr,textStatus,error) {
$('#dvMsg').text("Error:" + error + ",xhr: " + xhr + ",textStatus: " + textStatus);
}
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Enter First Number:</td>
<td>
<asp:TextBox ID="txtFirstNumber" runat="server" Text="22" /></td>
</tr>
<tr>
<td>Enter Second Number:</td>
<td>
<asp:TextBox ID="txtSecondNumber" runat="server" Text="33" /></td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btncalculate" Text="Calculate" runat="server" OnClientClick="return false;" />
<div id="dvMsg"></div>
</td>
</tr>
</table>
</div>
</form>
</body>
我做错了什么?
谢谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)