AJAX function not working in IIS 7 - asp.net

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)

Related

In ASP.NET , how can you make AJAX calls consistently to the same url?

So I have an ajax call like this:
$.ajax({
type: "GET",
url: "Home/GetFullView",
success: function (data) {
$("#sched").html(data);
},
fail: function () {
}
});
Where "Home" is the controller and "GetFullView" is the action. Sometimes the call works. However sometimes it crashes the application because it tries this url: "Home/Home/GetFullView". So it's adding "home" once too many times.
How can I consistently have it check the same url without doubling the "Home" controller name?
If I simply use url: "GetFullView" it also crashes because it looks for "Home" controller.
Introduce a leading /
url: "/Home/GetFullView",
you need a local url like:
url: "/Home/GetFullView"
To get your relative path fully qualified, use this:
url: "~/Home/GetFullView"
This will then give you the right qualified path whether you are at the website or application level.

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.

How to post parameter value to some actionlink

In my view i have 10 link every link associated with some unique value. Now i want that associated value at my controller action and from that action i want to redirect the flow to some other action based on that value.
But the condition is i dont want to display it on url.
How can i acheive this?
I tried ajax.post/#Ajax.ActionLink but doing this will not facilitate redirect to another action.
Is there anything with route i need to do?
View
<ul>#foreach (var item in Model)
{<li>
#Ajax.ActionLink(item.pk_name, "Index","Candidate", new { para1= item.para1 }
, new AjaxOptions { HttpMethod = "POST" })</li>
}</ul>
Action
[HttPost]
public ActionResult(int para1)
{
return RedirectToAction(para1,"anotherController");
}
I am getting value at para1 with ajax post(that is what i primarily needed) but here also want to redirect my application flow base on para1 value which is action name.
Confision : here i am not sure is this is the right way to do this thing. So i am asking you guys should i go for route map of working with ajax post will solve my objective.
If you only need to redirect the user based on what he clicks on without showing him the link, I believe the best way to achieve this is by client-side coding.
In my opinion there is no need to take any request through the server in order to change the page for such a low-complexity redirect.
View
// HTML
// Might be broken, been awhile since I worked with MVC
// Can't really remember if that's how you put variables in HTML
<ul id="button-list">
#foreach(var item in Model)
{
<li class="buttonish" data-para1="#item.para1">#item.pk_name</li>
}
</ul>
// JS
// I wouldn't do any server related work
$('#button-list li.buttonish').click(function(){
// Your controller and action just seem to redirect to another controller and send in the parameter
window.location.href = "/controller/method/" + $(this).data('para1');
});
I think you should make one jQuery function that is call when clicked and pass unique parameter.In that function you can use AJAX and post it on appropriate controller method.
Example:
<input type="button" id="#item.pk_name" onclick="getbuttonvalue(#item.para1);" />
In script
<script type="text/javascript">
$(document).ready(function () {
function getbuttonvalue(para1) {
$.ajax({
cache: false,
type: "POST",
dataType: 'json',
url: "/controller/method/" + para1,
success: function (data) {
}
});
}
});
</script>

Jquery Ajax Call does not Call the function in .CS file

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"

jQuery load() with callback function wont capture click()

I am having no luck in getting a jqueryui dialog to ajax load a form, which inturn submits via ajax.
Everything works upto the point of catching the form that is being submited and instead sending it through an ajax call. Thus the form action is triggered and the browser redirected. The ajax call is never made.
My code is as follows
$(document).ready(function() {
$('.viewOrder').click(function() {
$('#displayOrder').load(this.href, [], function() {
console.log("landed here");
$('#blah').click(function() {
console.log("submiting the form via ajax");
$.ajax({
url: "/ajax/orderupdate",
type: "GET",
data: data,
cache: false,
//success
success: function (data) {
console.log("worked:");
}
});
return false;
});
});
return false;
});
});
.viewOrder is the a href that is ajax loaded. This works fine.
I have read many similar questions on here and it seems load() does not execute scripts that are embeded in the return html, but my return code is pure html no scripts. Any ideas?
IMHO you should try and capture the submit instead of the click, that way you prevent submits done by keyboard aswell, and it might even fix your problem.
The events are bound on page load. At page load the form you are binding the click event does not exist. I use the livequery plugin but they added Live to jquery 4 which you can also use(i had some issues with IE so i went back to livequery)
So load livequery with your scripts http://docs.jquery.com/Plugins/livequery
and change
$('#orderUpdate').submit(function() {
to
$("#orderUpdate").livequery("submit", function() {

Resources