我認爲你需要一個.net webservice
。工作原理和你想要的不一樣。不是webform/MVC ..讓我們用WebService1.asmx和HTMLPage1.htm在同一個目錄下。
確保您取消註釋[System.Web.Script.Services.ScriptService]
WebService1.asmx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;
namespace StackOverflow_Solve.Services
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetMessage() // Home.CS
{
//return "GOGO";
Context.Response.Output.Write("Hello World");
Context.Response.End();
return string.Empty;
}
}
}
和HTMLPage1.htm
<!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>
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<head> //HTML and Ajax
<title></title>
<script>
function GetMessage() {
//Load jQuery($) to Use
$(function() {
$.get("WebService1.asmx/GetMessage", function (data) {
console.log(data);
$("p").html(data);
});
});
}
</script>
</head>
<body>
<input type="button" onclick="GetMessage()" value="Get Message" />
<p></p>
</body>
</body>
</html>
*不應該使用MVC行,Web窗體,API * **你想要什麼?爲什麼?** –
顯示'GetMessage'方法 –
嗨,請參閱上面的代碼。如果只用一個屬性就可以幫助我。如果沒有,請解釋爲什麼它不起作用! – Prashanth