首页 > 解决方案 > 使用 ajax asp.net 调用 Web 服务

问题描述

我有个问题:

我尝试调用用 VB.NET 编写的 Web 服务:

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports Newtonsoft.Json
<System.Web.Script.Services.ScriptService()>
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")>
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)>
<ToolboxItem(False)>
Public Class WebService1
    Inherits System.Web.Services.WebService

 <WebMethod()>
    Public Function Popola(cia As String) As Object

        Dim p1 As New Persona
        p1.cognome = cia
        p1.nome = "mario"
        p1.eta = 22

        Dim p2 As New Persona
        p2.cognome = "bianchi"
        p2.nome = "luca"
        p2.eta = 99

        Dim list As New List(Of Persona)
        list.Add(p1)
        list.Add(p2)

        Return JsonConvert.SerializeObject(list, Formatting.Indented)

    End Function

它之所以有效,是因为我在其他程序中使用它。

问题是在其他程序WebForm1.aspx中,我尝试用 Ajax 调用它

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication2.WebForm1" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" />
    <link href="StyleSheet1.css" rel="stylesheet" />
    

    

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>

    <script type="text/javascript">
        $(document).ready(function () {
            $("#button_login").click(function () {          // al click del bottone
                $.ajax({
                    type: "POST",                   // il method
                    url: "http://localhost/WebService1.asmx/Popola",                // la action

                    data: { cia: 88 },
                    contentType: "application/x-www-form-urlencoded",
                    dataType: "json",
                   
                    success: function () {
                        alert("ok")
                       

                    },
                   
                    error: function () {
                        console.log(arguments);
                        console.log("error");
                    }
                });

            });
        });
    </script>
</head>
<body>

    <form runat="server">
        <div class="container">
            <div class="row">
                <div class="col-sm-6">

                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    <asp:Button ID="button_login" runat="server" Text="Button" />

                </div>

            </div>
        </div>
    </form>
</body>
</html>

但每次它进入 Ajax 时,我都会从函数中得到一个错误:

从源“http://localhost”访问“http://localhost/WebService1.asmx/Popola”处的 XMLHttpRequest 已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头.

我能做些什么?

标签: asp.netajaxvb.netweb-services

解决方案


据我了解,您编写的代码是同一个项目的一部分。所以你可以像下面这样修改调用

更改下面的行

url: "http://localhost/WebService1.asmx/Popola",

url: "WebService1.asmx/Popola",

它应该工作。


推荐阅读