Sending an AJAX request from JavaScript to a user control in ASP.NET

JavaScript Function:

function CreateProposal(GetProposal, ProductName, ProductID) {

      $.ajax({
      type: "POST",
      url: "Page.ascx/Proposal",
      data: JSON.stringify({ GetProposal: GetProposal, ProductName: ProductName, ProductID: ProductID }),
      contentType: "application/json; charset=utf-8",
      dataType: "json",

      failure: function (response) {
          alert(response.d);
                                   }
     });

     }

Page.ascx Method for Proposal:

[WebMethod]
public static void Proposal(string GetProposal, string ProductName, string ProductID)
{

HttpContext.Current.Response.Redirect("MyPage");

}

Encountering Error when Posting Ajax to Proposal method:

POST http://localhost:63290/Page.ascx/Proposal 403 (Forbidden)

Need to identify the issue with my code. Any suggestions on what needs to be changed?

Answer №1

It is not possible to invoke this method because ascx controls do not represent a valid URL accessible from a client machine. They are intended for server-side embedding in other pages.

Instead, consider placing your method within an .aspx page and then calling the method from there.

$.ajax({
      type: "POST",
      url: "NewPage.aspx/Proposal",
      data: JSON.stringify({ GetProposal: GetProposal, ProductName: ProductName, ProductID: ProductID }),
      contentType: "application/json; charset=utf-8",
      dataType: "json",

      failure: function (response) {
          alert(response.d);
     }
});

Your NewPage.aspx must also have the same method implemented:

[System.Web.Services.WebMethod]
public static void Proposal(string GetProposal, string ProductName, string ProductID)
{

     HttpContext.Current.Response.Redirect("MyPage");

}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Building a Node.js authentication system for secure logins

Just diving into node.js and trying to create a user login system, but encountering an error when registering a new user: MongoDB Connected (node:12592) UnhandledPromiseRejectionWarning: TypeError: user is not a constructor at User.findOne.then.user ...

Show validation errors of a Django Form on submission by using jQuery's `.ajax()` function

Details My challenge involves incorporating a form within a modal window. The form's content is dynamically loaded using jQuery .load with the following code snippet: $('.modal-content').load('{% url 'edito' q.id %}');. ...

Efficiently explore a large dataset and display a dynamic "Loading..." message while searching asynchronously

I have been tasked with integrating a DataGridView that displays a large amount of data (currently 100,000 rows) and includes a search function. The grid has been implemented and the search function is working properly. However, when the CPU is performing ...

Having issues with Json stringification and serializing arrays

Having an issue with Json when using serializeArray. An example of my HTML form: <form action="" method="post" name="myForm"> ID: <input type="text" name="id" /><br/> State (XX): <input type="text" name="state" /><br/> <p ...

A method for calculating the average of values with identical keys within an object contained in an array

I have a JSON object that contains countries and data values. I need to add up the values of each country and compute their average. Here is the JSON object: var arraySeries = [{"country":"United States","data":2}, {"country":"Venezuela ...

Executing a Master Page Method with Jquery Ajax: My Approach

I'm encountering an issue when trying to invoke [WebMethod] from a Master Page using Jquery Ajax. An error message I'm receiving reads as follows: GetCompletionList (forbidden) The code snippet below is included in the Jquery section of the ...

Is it possible to incorporate a Try/Catch block within a QueuedTask.Run() operation?

Is it possible to include a Try/Catch block within the QueuedTask.Run() method? I have a hunch that the Try/Catch block should be placed outside of the QueuedTask.Run() method, but I am not entirely sure why. Can someone please explain whether it's b ...

Attempting to merge the data from two separate API responses into a single array of objects

I have a current project in which I'm dealing with an object that has three separate arrays of objects. Here's a glimpse of the structure... [ Array 1:[ { key: value} ], Array 2:[ { key: value}, { key: value} ], Array ...

What is the best way to display multiple results using ajax when fetching data from a json array?

Here is a query I am working on: $result = mysql_query("SELECT * FROM ship_data WHERE id = $ship") or die(mysql_error()); $rows = array(); while($r = mysql_fetch_assoc($result)) { $rows = $r; echo json_encode($rows); } Additionally, I have this snip ...

Press on a descendant element of an IWebElement using Selenium and C#

I have encountered an issue while looping through a list of Web elements and trying to click on each element individually. Despite passing the correct element as a parameter to a method during debug mode, the program repeatedly clicks on the first elemen ...

Dividing a set of information using Ajax

I am faced with a challenge where I have a list containing 4 data points from a Python script that is called by an Ajax function. The issue at hand is figuring out the method to separate this data because each piece of information needs to be sent to separ ...

Hugo: Utilizing template variables within JavaScript files

Within my Hugo website, the baseUrl configuration is set to: website.com/variable. If an image element is present in a .html file (such as < img src="/images/image.png" >), the baseUrl is appended resulting in the final URL being: website. ...

Delete any classes that start with a specific prefix

Within my code, there is a div that holds an id="a". Attached to this div are multiple classes from different groups, each with a unique prefix. I am uncertain about which specific class from the group is applied to the div in JavaScript. My goal is to r ...

Accessing an array beyond its callback scope

Currently, I am working on creating an array of objects with different key values by utilizing basic web scraping techniques in NodeJS. One challenge I am facing is accessing the 'built' Array outside of its parent function, which you can find he ...

Storing an array within an AngularJS service for better performance

As someone who is relatively new to AngularJS, I am still in the process of understanding how to utilize services for fetching data in my application. My aim here is to find a method to store the output of a $http.get() call that returns a JSON array. In ...

Having trouble closing the phonegap application using the Back Button on an Android device

I've encountered an issue with my code for exiting the application. It works perfectly the first time, but if I navigate to other screens and then return to the screen where I want to close the app, it doesn't work. <script type="text/javascr ...

The Yeoman/Grunt-usemin is failing to update the index.html file even after adding new JavaScript code

As I work on developing a web app using Yeoman and the Angular generator on Windows 7, I go through the process of running 'yo angular' followed by 'grunt' to build the app for deployment. The index.html file in the dist folder gets upd ...

The setInterval() function is not functioning properly when used with PHP's file_get_contents

Currently, I'm encountering an issue while attempting to use the function get_file_contents() within a setInterval(). The objective is to continuously update some text that displays the contents of a file. Below is the code snippet: <script src="h ...

Enable individuals to mark coordinates on a Google Map using PHP and Ajax

My goal is to integrate a map feature on my website where users can click on the map to add specific information to certain points. I have successfully set up a Google map and displayed points from a database, but I am unsure how to enable users to add the ...

Trouble with PUT request for updating user password using Express.js with mongoose

I've encountered a puzzling issue while working on my Express.js application. Specifically, I have created an endpoint for updating a user's password. Surprisingly, the endpoint functions flawlessly with a POST request, but fails to work when swi ...