当我使用 Ajax 从 asp.net 核心 MVC 调用 asp.net webforms 中的函数时没有达到目标?
When I call a function in asp.net webforms from asp.net core MVC using Ajax is not hitting the target?
我有两个项目,例如 Dot net Core MVC 中的主项目和 ASP.NET 框架 webforms 中的目标项目我在目标项目的 Webform 中有一个测试方法,但我想从主项目访问该方法使用jQuery Ajax 但是这段代码没有命中目标方法..
来自主项目
$('#btnTest').click(function () {
AjaxCall();
});
function AjaxCall() {
$.ajax({
type: 'GET',
crossDomain: true,
dataType: 'jsonp',
url: 'https://localhost:44332/WebForm1.aspx/TestMethod',
success: function (jsondata) {
alert('Success');
},
error: function (request, error) {
alert("Failed");
}
})
}
</script>
//Target Project Code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
namespace TargetProject
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string TestMethod()
{
return "Success";
}
}
}
出于安全原因,ASP.NET AJAX 页面方法仅支持 POST 请求。如果将方法更改为 POST
,则无法使用 JSONP
作为数据类型。
更改下面 ASP.NET Core
中的代码:
<script>
$('#btnTest').click(function () {
AjaxCall();
});
function AjaxCall() {
$.ajax({
type: 'POST', //change here.....
crossDomain: true,
dataType: 'json', //change here.....
contentType: "application/json; charset=utf-8", //add this...
url: 'https://localhost:44332/WebForm1.aspx/TestMethod',
success: function (jsondata) {
alert('Success');
},
error: function (request, error) {
alert("Failed");
}
})
}
</script>
请确保您已在 WebForms
中的 web.config
文件中配置 cors:
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Headers" value="accept, content-type" />
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="POST, GET, OPTIONS" />
</customHeaders>
</httpProtocol>
</system.webServer>
//.....
</configuration>
确保在 WebForms
中的 App_Start
文件夹中更改 RouteConfig.cs
:
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
var settings = new FriendlyUrlSettings();
//settings.AutoRedirectMode = RedirectMode.Permanent;
settings.AutoRedirectMode = RedirectMode.Off;
routes.EnableFriendlyUrls(settings);
}
}
我有两个项目,例如 Dot net Core MVC 中的主项目和 ASP.NET 框架 webforms 中的目标项目我在目标项目的 Webform 中有一个测试方法,但我想从主项目访问该方法使用jQuery Ajax 但是这段代码没有命中目标方法..
来自主项目
$('#btnTest').click(function () {
AjaxCall();
});
function AjaxCall() {
$.ajax({
type: 'GET',
crossDomain: true,
dataType: 'jsonp',
url: 'https://localhost:44332/WebForm1.aspx/TestMethod',
success: function (jsondata) {
alert('Success');
},
error: function (request, error) {
alert("Failed");
}
})
}
</script>
//Target Project Code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
namespace TargetProject
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string TestMethod()
{
return "Success";
}
}
}
ASP.NET AJAX 页面方法仅支持 POST 请求。如果将方法更改为 POST
,则无法使用 JSONP
作为数据类型。
更改下面 ASP.NET Core
中的代码:
<script>
$('#btnTest').click(function () {
AjaxCall();
});
function AjaxCall() {
$.ajax({
type: 'POST', //change here.....
crossDomain: true,
dataType: 'json', //change here.....
contentType: "application/json; charset=utf-8", //add this...
url: 'https://localhost:44332/WebForm1.aspx/TestMethod',
success: function (jsondata) {
alert('Success');
},
error: function (request, error) {
alert("Failed");
}
})
}
</script>
请确保您已在 WebForms
中的 web.config
文件中配置 cors:
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Headers" value="accept, content-type" />
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="POST, GET, OPTIONS" />
</customHeaders>
</httpProtocol>
</system.webServer>
//.....
</configuration>
确保在 WebForms
中的 App_Start
文件夹中更改 RouteConfig.cs
:
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
var settings = new FriendlyUrlSettings();
//settings.AutoRedirectMode = RedirectMode.Permanent;
settings.AutoRedirectMode = RedirectMode.Off;
routes.EnableFriendlyUrls(settings);
}
}