1

I have two project such as Main Project in Dot net Core MVC and Target project in ASP.NET framework webforms I have a Test Method in Webform in Target Project , But I want access the method from Main project using jQuery Ajax But this Code Does not hit the target Method..

From Main Project

    $('#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";
        }
    }
} 

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

NOUFAL KP
  • 13
  • 4
  • You can only post to a WebMethod. See this stack overflow [article](https://stackoverflow.com/questions/2397466/ajax-get-requests-to-an-asp-net-page-method/2397521#2397521) – Rich Wagenknecht Apr 14 '22 at 16:24

1 Answers1

0

ASP.NET AJAX page methods only support POST requests for security reasons. If you change the method to POST, you can't use JSONP as dataType.

Change your code in ASP.NET Core below:

<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>

Be sure you have configured cors in web.config file in WebForms:

<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>

Be sure change your RouteConfig.cs in App_Start folder in WebForms:

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);
    }
}
Rena
  • 30,832
  • 6
  • 37
  • 72
  • where I need to Add settings.AutoRedirectMode = RedirectMode.Off; because in dot net core does not have RouteConfig.cs – NOUFAL KP Apr 15 '22 at 04:13
  • Hi @NOUFALKP, RouteConfig.cs is in `App_Start` folders in WebForms project. – Rena Apr 15 '22 at 05:02
  • Webforms Project Does not have Any `App_Start` folder, I have Added an Image of Solution Explorer in the Question ,Could you please check that.. – NOUFAL KP Apr 15 '22 at 07:57
  • Hi @NOUFALKP, I think you may create a ASP.NET empty template instead of WebForms, you need learn how to create a WebForms template. Refer to:https://learn.microsoft.com/en-us/aspnet/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/create-the-project. – Rena Apr 15 '22 at 08:40
  • Again I have Updated my Project as per your Code, Please check the updated images on the Questions ,But A gain it is Not hitting the target – NOUFAL KP Apr 18 '22 at 07:32
  • It is to change RouteConfig.cs code to: `settings.AutoRedirectMode = RedirectMode.Off`, pls remove `settings.AutoRedirectMode = RedirectMode.Permanent`. – Rena Apr 18 '22 at 08:01
  • Hi @Rena - I think it is Due to Connection Refused Issue , See the Last Image I have Added, Actually I am using `DevextreamWebApplication` as the Main Project ,So that I have seen this Error , While I changed the `Normal DotnetCore Web Application` it not getting this Error and it is Working Fine but I have using `Devextream` as My Working Solution , so I have this Connection Refused Error and that is why it is not hitting the Target. – NOUFAL KP Apr 18 '22 at 08:36
  • This error is caused by `settings.AutoRedirectMode = RedirectMode.Permanent` or you write the wrong request url. – Rena Apr 18 '22 at 09:26
  • I have Already commented the line `settings.AutoRedirectMode = RedirectMode.Permanent` and Added `settings.AutoRedirectMode = RedirectMode.Off` but it is still show the Error – NOUFAL KP Apr 18 '22 at 09:55
  • Now it is Working, I think that the my Port Number has Changed, Now it is Working.. Thank you soo much for your valuable effort and Time @Rena – NOUFAL KP Apr 18 '22 at 10:26