簡體   English   中英

如何在不使用 MVC、WebForms 概念的情況下通過 jQuery Ajax 調用 c# 類中的方法

[英]How to call a method in c# class by jQuery Ajax without using MVC,WebForms concepts

我需要使用屬性 [WebMethod] 調用 C# 方法,它不應該使用 MVC、WebForms、API。 它應該是一個干凈的 c# 類 (.CS)、HTML 文件。

這是我的WebMethod

[WebMethod]
public string GetMessage()   // Home.CS 
{           
        return "GOGO";
}    

這是我的ajax代碼:

<head> //HTML and Ajax 
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
 <script>
    function GetMessage() {
        $.get("/Home/GetMessage", function (data) {
            $("p").html(data);
        });
    }
 </script>
</head>
<body>
    <input type="button" onclick="GetMessage()" value="Get Message" />
    <p></p>
</body>

我知道這可能不是您正在尋找的答案。 但是從你的問題來看,在我看來你真的不知道什么是客戶端 - 服務器架構,什么是服務器,什么是客戶端。

我建議您了解這些層,然后嘗試為您的情況找到解決方案。

您問題的直接答案是“不可能”。 但要明白為什么? 您需要了解客戶端-服務器系統的架構。 你可以從這里開始——

https://en.wikipedia.org/wiki/Client%E2%80%93server_model

IIS 的特定鏈接 -

https://msdn.microsoft.com/en-us/library/ms178473.aspx

https://msdn.microsoft.com/en-us/library/bb470252.aspx

Asp.net頁面生命周期——

https://msdn.microsoft.com/en-us/library/ms178472.aspx

我認為你需要一個 .net webservice .Works 和你想要的一樣..not webform/MVC ..Let 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM