Jquery Ajax Call does not Call the function in .CS file - asp.net

Always i get the alert in the "error". When i debugged i get the type,url as undefined. can anyone help me why that method is not getting called??
$(document).ready(function () {
$("#btnajaxcall").click(function () {
$.ajax({
type: "POST",
url: "Default.aspx/jQueryAjaxCalledMethod",
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: 'json',
success: function () { alert('success') },
error: function () { debugger; alert('failure'); return false; }
});
});
});
[WebMethod]
public void jQueryAjaxCalledMethod()
{
//SOME CODE HERE
}

If im correct you should be using static method for these purposes, so function in your code behind should look like this
[WebMethod]
public static void jQueryAjaxCalledMethod()
{
//SOME CODE HERE
}
If you still get some errors take a look on this guy blog Encosia maybe you'll find there a solution

The jquery Ajax method is going to post your data in json format using the plain html protocol. ASP.NET will be expecting to unwrap a SOAP request to pass to the webmethod. Thus the error. You should use an MVC action instead, as suggested in one of the comments.
EDIT:On further investigation ASP.Net has an attribute that will allow the web method to be called:
[System.Web.Script.Services.ScriptService].
Use this attribute on the class and it might solve your problem.

Hi all i just used the jquery file hosted with google.
It worked out fine.
Previously i was using the jquery version 1.7.1 that i had downloaded and stored in my local. I also saw a lot of questions in the forum that this particular ajax call is quite not happening properly with .NET 4. I am not sure and forgive me if i am wrong but i do have a feeling that 1.7.1 in this case is not properly working with ASP.NET 4.
P.S -> I used this in the script tag -->
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"

Related

web method using jqueryajax in asp.net not working in Mozilla firefox

I am working on asp.net website in which i am getting data by using jquery ajax.
here is my code.
function PostSubChapter(qbt_id) {
debugger;
var v1 = 'qbt_id:' + qbt_id;
$.ajax(
{
type: "POST",
url: '<%= ResolveUrl("~/QuestionBankSubChapters.aspx/GetChapters") %>',
data: '{' + v1 + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
if (result.status === "OK") {
alert('Comment posted');
}
else {
fnDisplaySubChapter(result, qbt_id);
}
},
error: function (req, status, error) {
alert("Sorry! Post failed due to error");
}
});
}
and my web method is
[WebMethod]
public static List<BO.QuestionBankSubChapters> GetChapters(int qbt_id)
{
BAL.QuestionBankSubChapters oQBTSC = new BAL.QuestionBankSubChapters();
List<BO.QuestionBankSubChapters> lstQBTSC = oQBTSC.getQuestionBankSubChapters(qbt_id);
return lstQBTSC;
}
These is working very fine in chrome an IE but not showing any result in case of FireFox
call is going properly to web method but at the time of getting result from it is calling Error function.It has to call another function present in sucess function but not calling that function.
Pls help me these.
for your understanding here i am mentioning the link of the website
Link is : "http://skillgun.com/Home.aspx"
Open these in Fire fox and chrome both then Click on Arithmetic then it will display 2nd screen.
Just see the o/p in both the browser you'll understand. In chrome its working properly but in Firefox its not.I am not getting understand what is the reason behind these.Sample code already i mentioned above.
I am using jquery-1.8.3.min.js for getting the result.The callback function is working fine in chrome and IE but not working in FF
Please help me....
Your page method returns correct response, problem is with your other code that you've not posted but I could see in FF console.
You've used innerText at many places and FF doesn't support it. Try innerHTML instead.
For waitprocess div you're not using # along with it's id while using Jquery Selector $ and hence it is visible all the time.

AJAX function not working in IIS 7

I have an asp mvc 3 application, and there is a view that makes an ajax call, when I run it in visualstudio it workes but when i run it in IIS 7 it is not sending it to the server! I searched for a solution and it said that the urls had to be modified, so i changed it like this using url action but it still doesn't do anything, does anybody know why this might be?
In the webpage I don't see anymessage it simply doesn't do anything.
The ajax funciton is inside the code of the view, it is embedded there, it looks like:
<script type="text/javascript">
function display(Txt) {
$.ajax({
type: "POST",
//url: "/Controller/Action",
url: '#Url.Action("Controller", "Action")',
data: "Id=" + Txt,
success: function (result) {
if (result.Info != undefined) {
//do something
}
else if (result.Info == undefined) {
//do something
}
}
});
}
</script>
The problem was that Url.Action was the other way around:
before:
url: '#Url.Action("Controller", "Action")',
after:
url: '#Url.Action("Action", "Controller")',
This is strange because I checked a blog from microsoft and they had it in the first order =S
First try getting to the Ajax uri in your browser.
If you cannot you may just have set the app up in a different folder structure.
If your controller method has an Ajax attribute remove it for this test.
The answer may be apparent after trying the url (uri)

mvc with ajax post

i have a form which includes list of records. When user clicks edit image on table modal div will show to him. i get this modal div with ajax. Now after changing some fields i am posting it via ajax. I watched to firebug. it sends parameter. But when i debug code in VS method calls but no parameter has been send. i have done it before in other pages. but now i can not. What problem can be here in my code?
C# Code here
[HttpPost]
//[Authorize(Roles = "Operator")]
public ActionResult EditRow(string Name, string SecondName)
{
//code goes here
return Content("Saved");
}
jquery ajax code is here
$.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
url: 'EditRow',
data: { Name: "php", SecondName: "MVC" },
dataType: 'html',
success: function (response) {
//some code goes here
}
});
Have you tried to simplify your Ajax call to:
$.ajax({
type: 'POST',
url: 'EditRow',
data: { Name: "php", SecondName: "MVC" },
success: function (response) {
//some code goes here
}
});
If this doesn't work there must be something else in your code that makes your code invalid. Maybe some base controller action filters you forgot to put there or some custom model binders or some other global registration.
Sending complex JSON to server
If you'd like to send complex JSON using the same technique, you can read my blog post and use the simple plugin that will make it possible to send complex JSON objects to Asp.net MVC controller action.

Why do I need to use .d to access data returned by jQuery AJAX?

I've put together some jQuery AJAX code using some tutorials I found on the internet. I'm new to jQuery and want to learn how to do things betters. I have a coworker who put together a beautiful web application using a lot of jQuery.
The thing I'm most confused about here is: why is it necessary to use the ".d" when referring to the response of my web method and what does it stand for?
// ASP.net C# code
[System.Web.Services.WebMethod]
public static string hello()
{
return ("howdy");
}
// Javascript code
function testMethod() {
$.ajax({
type: "POST",
url: "ViewNamesAndNumbers.aspx/hello",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert(msg); // This doesn't display the response.
alert(msg.d); // This displays the response.
} // end success:
}) // end $.ajax
It was added in ASP.NET 3.5’s version of ASP.NET AJAX to prevent you from being vulnerable to this exploit: http://haacked.com/archive/2009/06/25/json-hijacking.aspx
(Answer sourced from http://encosia.com/2009/06/29/never-worry-about-asp-net-ajaxs-d-again/)
Microsoft does this to protect you from a security exploit. See the bottom of This Page for more information.
I guess alert(msg) displays "[object Object]" ?
If so it's because the object which is parsed through window.JSON (which happens under the hood when specifying json as dataType) does really look:
object = {
d: "some data"
}
Check what you are generating in ViewNamesAndNumbers.aspx/hello

How to refresh particular part of my web page?

I want to refresh only a single part of my page not the whole.
How ?
I wouldn't recommend Update Panel but you can use jQuery $.load() method which is pretty slick. Once you start using it, it helps you a lot.
So use Ajax... long story short.
Edit :
Maybe this article help as well :
Why Update Panels are Dangerous :
http://encosia.com/2007/07/11/why-aspnet-ajax-updatepanels-are-dangerous/
In web forms you can use the update panel
See here for example
You can also use JQuery although it depends on what you are trying to do and to what complexity.
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "YourPage.aspx/apply",
dataType: "json",
data: json.stringify(""),
success: function (result) {
alert(result);
// window.location.href = "ClubCreation.aspx";
},
Error: function () {
alert('error');
}
});
[HttpPost]
public string apply()
{
return "Hi";
}
You can use Ajax to solve your problem this code will help you

Resources