JQuery在asp.net中三種ajax傳值

來源:互聯網
上載者:User

標籤:

1)通過webservice,注意去掉注釋[System.Web.Script.Services.ScriptService]這行前的注釋

2)通過aspx.cs檔案中的靜態方法

3)通過aspx檔案url

 

WebForm1.aspx

WebForm1.aspx

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="asp.net.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
function Ws() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "WebService1.asmx/HelloWorld2",
data: "{name:‘xiaoxiao‘}",
dataType: ‘json‘,
success: function (result) {
alert(result.d);
}
});
}
function StaticMethod() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "aspxpage.aspx/SayHello2",
data: "{name:‘xiaoxiao‘}",
dataType: ‘json‘,
success: function (result) {
alert(result.d);
}
});

}
function FromPage() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "dataContent.aspx?nowtime=‘" + new Date() + "‘",
data: "{}",
dataType: ‘html‘,
success: function (result) {
alert(result);
}
});

}

</script>
</head>
<body>
<form id="form1" runat="server">

<div>
<input id="Button1" type="button" value="jquery調用WebService" onclick="Ws()" />
</div>
<div>
<input id="Button2" type="button" value="jquery調用aspx頁面靜態方法" onclick="StaticMethod()" />
</div>
<div>
<input id="Button3" type="button" value="jquery通過page儲存值" onclick="FromPage()" />
</div>
</form>
</body>
</html>

以上是啟動頁面,WebForm1.aspx.cs沒有代碼

WebService1.asmx

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace asp.net
{
/// <summary>
/// WebService1 的摘要說明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允許使用 ASP.NET AJAX 從指令碼中調用此 Web 服務,請取消對下行的注釋。
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{

[WebMethod]
public string HelloWorld()
{
return "Hello World"+System.DateTime.Now.ToLongTimeString();
}

[WebMethod]
public string HelloWorld2(string name)
{
return "Hello World" + name + System.DateTime.Now.ToLongTimeString();
}
}
}

 

以上是webservice中的代碼

 

aspxpage.aspx.cs

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->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;

namespace asp.net
{
public partial class aspx頁面代替ws : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
[WebMethod]
public static string SayHello()
{
return "Hello";
}

[WebMethod]
public static string SayHello2(string name)
{
return "Hello"+name;
}
}
}

 

以上是針對第二條 通過aspx.cs中的靜態方法 注意方法前要加 [WebMethod],aspxpage.aspx頁面沒代碼.

 

dataContent.aspx.cs

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace asp.net
{
public partial class dataContent : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Page.ViewStateMode = ViewStateMode.Disabled;
if (Request.QueryString["nowtime"] != null)
{
string stime = Request.QueryString["nowtime"].ToString();
Response.Write(stime);
}
Response.Flush();

}
}
}

 

以上是針對第三條 用url傳值 通過aspx頁面儲存資料。dataContent.aspx頁面沒有代碼.

JQuery在asp.net中三種ajax傳值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.