简体   繁体   English

使用jquery ajax从C#中的类调用函数

[英]Call function from class in c# using jquery ajax

I have been able to call the function using ajax using following code 我已经能够使用以下代码使用ajax调用该函数

function AjaxCall() {
    $.ajax(
    {
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        url: 'Default.aspx/Server_HelloWorld',
        datatype: 'json',
        success: function (result) { alert(result.d); }
    })

}

using System.Web.Services;
using System.Web.Script.Services;

aspx.cs Ajax Webmethod, aspx.cs Ajax Webmethod,

[WebMethod]
public String Server_HelloWorld()
{
    return "Hello, How are you?";
}

But if i don't have any .aspx page, i have only .html page with a class say myfunclass.cs . 但是,如果我没有任何.aspx页面,那么我只有.html页面,其类为myfunclass.cs How do i call the same function from here. 我如何从这里调用相同的功能。 I have tried but fail 我已经尝试过但是失败了

 $.ajax(
    {
        type: 'POST',
        contentType: "application/json; charset=utf-8",
        url: 'myfunclass.cs/Server_HelloWorld',
        datatype: 'json',
        success: function (result) { alert(result.d); }
    })

You cant just call *.cs file. 您不能只调用* .cs文件。 You need to create some backend, like ASP.NET MVC Web API like in example here 您需要创建一些后端,例如此处示例中的ASP.NET MVC Web API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM