【问题标题】:Call Multiple function in Web Service using AJAX in ASP.NET在 ASP.NET 中使用 AJAX 在 Web 服务中调用多个函数
【发布时间】:2021-04-14 22:55:49
【问题描述】:
$(document).ready(function () {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/Services.asmx/HelloWorld",
        data: "{}",
        dataType: "json",
        success: function (data) {
            alert(data.d);
        },
        error: function (err) {
            alert(err.d);;
        }
    });
});

当我调用函数HelloWorld 时,它工作正常。但问题是我在这个文件中有多个函数,比如HelloWorld2、HelloWorld3。代码如下:

Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System
Imports System.Collections.Generic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Script.Services

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _

Public Class Services
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function HelloWorld() As String
        Return "Hello Admin"
    End Function
    Public Function HelloWorld2() As String
        Return "Hello Manager"
    End Function
    Public Function HelloWorld3() As String
        Return "Hello User"
    End Function
End Class

如何调用HelloWorld2 函数,例如:

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "/Services.asmx/HelloWorld2",
    data: "{}",
    dataType: "json",
    success: function (data) {
        alert(data.d);
    },
    error: function (err) {
        alert(err.d);;
    }
});

【问题讨论】:

  • 你的函数看起来都一样,你的调用看起来都一样,所以你可以用同样的方式调用其他函数。您面临的具体问题是什么?

标签: asp.net ajax web-services


【解决方案1】:

请按如下方式使用此代码

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "/Services.asmx/HelloWorld",
    data: "{}",
    dataType: "json",
    success: function (data) {
        alert(data.d);
    },
    error: function (err) {
        alert(err.d);;
    },
    complete: function(data) {
        // complete web service 1
        //A function to be called when the request finishes 
        // (after success and error callbacks are executed). 
        // call web service 2
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/Services.asmx/HelloWorld2",
            data: "{}",
            dataType: "json",
            success: function (data2) {
                alert(data2.d);
            },
            error: function (err2) {
                alert(err2.d);;
            },
            complete: function(data2) {
                // complete web service 2
                // call web service 3
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "/Services.asmx/HelloWorld3",
                    data: "{}",
                    dataType: "json",
                    success: function (data3) {
                        alert(data3.d);
                    },
                    error: function (err3) {
                        alert(err3.d);;
                    },
                    complete: function(data3) {
                        // complete web service 3
                    }
                });
            }
        });
    }
});

【讨论】:

  • 我不想同时调用所有函数。我的问题是只有文件的第一个功能在工作。它是 HelloWorld() 或 HelloWorld2() 或 HelloWorld3()
  • 将此信息添加到您的原始问题中。说明您希望何时以及如何调用不同的函数、您尝试过的内容以及无效的内容。
猜你喜欢
  • 1970-01-01
  • 2017-09-23
  • 1970-01-01
  • 2021-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-11
相关资源
最近更新 更多