I have methos to display content of page at server side like
DisplayDetails(Page PageNema)
{
///
}
How can i call this function from javascript as well as how can i pass Page argument from Javascript
If it's WebForm you must set this method as WebMethod, so then you can call this method from jQuery. Something like that:
Client Side.
$.ajax({
type: "POST",
url: "PageName.aspx/MethodName",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Do something interesting here.
}
});
Server side:
public partial class _Default : Page
{
[WebMethod]
public static string DisplayDetails()
{
//your code to retrieve details here
return Details;
}
}
[WebMethod]
public static string DisplayDetails(parameter1,parameter2,etc...)
{
return something;
}
Client side code
<script type="text/javascript">
function functionName(callback) {
$.ajax({
type: "POST",
url: 'PageName.aspx/DisplayDetails',// your function name
data: '{"argument1","argument2",etc...}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
// If u want something from serverside function then write your code here
},
error: function (e) {
}
});
}
</script >
Related
I'm using web-forms to collect data from a form and then send the data to the code-behind to send to an API. Using a button I'm calling a JavaScript method which collates the data and then sends to my aspx.cs file to send to the API. The Html code for the button is
<button class="search btn" ID="btnSearch" onclick="searchApi(); return false;"><i class="fas fa-search"></i>Search</button>
This runs the searchAPI() function which works and creates a concatenated string called SearchData. The Javascript code looks like this
var searchString = JsonData;
var trimsearchString = searchString.replace(/,/g, '');
$.ajax({
type: "POST",
url: 'Default.aspx/GetApi',
data: searchString,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert('success');
},
error: function (errordata) {
console.log(errordata);
alert(errordata);
}
});
The method GetAPI in my Default.aspx.cs file is never called. The method code is
[System.Web.Services.WebMethod]
public static void GetApi(string searchData)
{...
The success: function (data) returns success but the code behind method is never called, can someone please tell me what I am missing.
fix the ajax data, it seems that method with this parameter can' t be found
$.ajax({
type: "POST",
url: 'Default.aspx/GetApi',
data: { searchData: trimsearchString},
//or if it is still problem, you can even try
data: JSON.stringify( { searchData: trimsearchString}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data);
},
error: function (errordata) {
console.log(errordata);
alert(errordata);
}
});
and your webmethod should return something
[WebMethod]
public static string GetApi(string searchData)
{
return searchData
}
I am trying to submit an ajax request from an external JavaScript file in ASP.NET MVC. I'm getting a 500. What am I doing wrong?
Ajax call (From external JS file)
$.ajax({
type: "POST",
url: '/Home/AjaxEndpoint',
data: { jsonData: "testing" },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: successFunc,
error: errorFunc
});
Controller Action Method (That should be catching the request)
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpGet]
public void AjaxEndpoint()
{
var thing = 1 + 2;
}
// AJAX endpoint for GetProducts.js
[HttpPost]
public void AjaxEndpoint(string jsonData)
{
var thing = 1 + 2;
}
}
Error I'm getting
You either need to remove the contentType option
$.ajax({
type: "POST",
url: '/Home/AjaxEndpoint',
data: { jsonData: "testing" },
dataType: "json",
success: successFunc,
error: errorFunc
});
or alternatively, stringify the data
$.ajax({
type: "POST",
url: '/Home/AjaxEndpoint',
data: JSON.stringify({ jsonData: "testing" }),// modify
contentType: "application/json; charset=utf-8",
dataType: "json",
success: successFunc,
error: errorFunc
});
Your ajax call needs to be to an ActionResult in the controller, The controller performs and returns the data to the page
[HttpPost]
public ActionResult ajaxcall(string ids)
{
String Data = code to perform
return Json(Data);
}
this is the basic idea. Javascript makes the call and uses the json data returned on the clients page
I have solution structure as:
I want to call recommendationProcess function from CTL_RateRecommendationDetails.ascx.cs from
CTL_RateRecommendationDetails.ascx
So i written code as:
$.ajax({
type: "POST",
url: "/UserControls/CTL_RateRecommendationDetails.ascx/recommendationProcess",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(value) {
alert(value.d);
},
error: function(e) {
alert("Ajax Error" + JSON.stringify(e));
}
});
But every time it comes in error block.
I have tried with :
url: "/CTL_RateRecommendationDetails.ascx/recommendationProcess",
And
url: "CTL_RateRecommendationDetails.ascx/recommendationProcess",
But its not calling function.
[System.Web.Services.WebMethod]
public static void recommendationProcess()
{
}
You can't call code behinds of user controls from JQuery Ajax
Ref : http://forums.asp.net/t/1423555.aspx
Using jQuery AJAX to call ASP.NET function in control code-behind instead of page code-behind
how-to-access-server-side-method-in-ascx-control-using-jquery-ajax-method
Im trying to call a c# method on select changed event of a dropdown list,The select change event triggers but the ajax does not work
<script type="text/javascript">
$(document).ready(function () {
$('body').delegate('#drpselect1', 'change', function () {
var groupname = $("#drpselect1 option:selected").text();
alert(groupname);
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "sample.aspx/getdata",
dataType: "json",
{"text":groupname},
success: function () {
alert("works");
// window.location.href = "ClubCreation.aspx";
},
Error: function () {
alert('error');
}
});
/* $.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "sample.aspx/getdata",
data:{"text":groupname}
dataType: "json",
success: function () {
alert('Successfully Saved');
//window.location.href = "ClubCreation.aspx";
},
Error: function () {
}
});*/
});
});
</script>
c# method
[WebMethod]
public static void getdata(String text)
{
//do stuff
}
You have to decorate getdata method with
[WebMethod] attribute.
In your c# code [WebMethod] is missing.
try this
check this line
data:'{"text":"'+groupname+'"}',//put "data:"
now,
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "sample.aspx/getdata",
dataType: "json",
data:'{"text":"'+groupname+'"}',//put "data:"
success: function () {
alert("works");
// window.location.href = "ClubCreation.aspx";
},
Error: function () {
alert('error');
}
});
Probabky you missing attributes:
[System.Web.Services.WebMethod()]
public static void getdata(String text)
Look here for more informations: Using jQuery to directly call ASP.NET AJAX page methods
I want to post data from dynatree to my asp.net mvc server via ajax. I use the model classes (Dynatree with ASP.NET MVC) from Steve which work well for getting data from the server to the client. But i have still problems with posting the tree data to the server.
Client:
var td = $("#tree").dynatree("getTree").toDict();
var json = JSON.stringify(td);
$.ajax({
type: "POST",
url: "/parttree",
data: json,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response);
}
});
Server:
[POST("/parttree")]
public ActionResult TreeData2( List<TreeItem> ot)
{
// ot is always null here
}
Content of json in VS debugger:
{"title":null,"key":"_1","isFolder":false,"isLazy":true,"tooltip":null,"href":null,"icon":null,"addClass":null,"noLink":false,"activate":false,"focus":false,"expand":true,"select":false,"hideCheckbox":false,"unselectable":false,"children":[{"title":"root","key":"_2","isFolder":false,"isLazy":false,"tooltip":null,"href":null,"icon":null,"addClass":null,"noLink":false,"activate":false,"focus":false,"expand":false,"select":false,"hideCheckbox":false,"unselectable":false,"children":....
I would say that it would be the following :
$.ajax({
type: "POST",
url: "/parttree",
data: {tree: json},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response);
}
});
And your MVC Action would be :
[HttpPost]
public ActionResult PartTree(FormCollection form)
{
List<TreeItem> ot = new JavaScriptSerializer().Deserialize<List<TreeItem>>(form["tree"]);
}
Although you are probably looking to a JsonResult not an ActionResult if you are returning JSON.