I have a problem:
I try to call a web service written in VB.NET:
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
It works because I use it in other programs.
The problem is when in a other program WebForm1.aspx
, I try to call it with 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>
but every time it enters into Ajax, I get an error from the function:
Access to XMLHttpRequest at 'http://localhost/WebService1.asmx/Popola' from origin 'http://localhost' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
What can I do?