this is my sample code:
$("#btnPost").click(function (e) {
e.preventDefault();
$.ajax(
{
url: "/api/Flight/FlightList",
type: "Post",
dataType: "json",
data: $('form').serialize() + '&' + $.param({ 'TokenId': $("#pageInitCounter").val() }, true),
// data:"",
success: function (data) {
// implementation here
........................
As, u can see if i click button then it should hit controller and post the form data but it is not working.it was working fine in older chrome version.
but yesterday i updated my chrome browser. after that its not working.
if i try this:
data:"",
then it is hitting controller but with empty data. how it should be done.please someone tell me.
i am using jquery version 1.9.1
In the ajax operation just add
async: true,
after
datatype: "json",
and that should solve your problem. Chrome has issue handling asynchronus calls.
$("#btnPost").click(function (e) {
e.preventDefault();
$.ajax(
{
url: "/api/Flight/FlightList",
type: "Post",
dataType: "json",
async: true,
data: $('form').serialize() + '&' + $.param({ 'TokenId': $("#pageInitCounter").val() }, true),
// data:"",
success: function (data) {
// implementation here
........................
Note: async parameter is bydefault true, so you need not pass that with request, so removing async: false will also do it for you.
Related
I have a .net project in the framework 4.5.1
When I use ajax like below, it works and the debugger hits the page_load event:
$.ajax({
"async": true,
"crossDomain": true,
type: "POST",
url: "Advanced.aspx",
contentType: "application/json; charset=utf-8",
success: function (ms) {
alert('success');
}
})
In my advanced.aspx.cs, I have the method below:
[System.Web.Services.WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
public static string SaveChange(string myval)
{
//dummy code for test purposes.
string u = myval + "X";
return u;
}
I want to hit this method via ajax. To do this, I am updating my ajax call like below:
$.ajax({
"async": true,
"crossDomain": true,
type: "POST",
url: "Advanced.aspx/SaveChange",
data: {username:"test"},
contentType: "application/json; charset=utf-8",
success: function (ms) {
alert('success');
}
})
However, it gives me 401:unauthorized error when i do it in that way, shown below:
How can I fix this? Tried lots of things for the last few hours, no luck.. Any help would be appreciated.
EDIT: I've used async and crossDomain hoping to fix this issue, didn't help. Normally they don't exist in my ajax call, nothing changes.
Found the answer here: ASP.NET Calling WebMethod with jQuery AJAX "401 (Unauthorized)"
I have used
settings.AutoRedirectMode = RedirectMode.Off; instead of RedirectMode.Permanent in ~/App_Start/RouteConfig.cs, which fixed the issue.
Try updating your AJAX JS to:
$.ajax({
type: "POST",
url: "Advanced.aspx/SaveChange",
data: {myval:"test"},
success: function (ms) {
alert('success');
}
})
I have used ajax calling to get details from database this ajax calling works fine in local but after uploading on server I always get error Resource Cannot be found I have tried many things depend on the solution on the web but no one succeed I even use to create details view and works fine on local but the same error after upload
i have tried
url: '/MobileS/Get/' + MobileSID
url: '~/MobileS/Get/' + MobileSID
url: '../MobileS/Get/' + MobileSID
and also i have tried despite its wrong but i think it may be work
url: '../../MobileS/Get/' + MobileSID
url: '~/~/MobileS/Get/' + MobileSID
url: 'MobileS/Get/' + MobileSID
this is the main function
function getbyID(MobileSID) {
$('#Name').css('border-color', 'lightgrey');
$.ajax({
url: '/MobileS/Get/' + MobileSID,
dataType: "json",
type: "GET",
contentType: 'application/json; charset=utf-8',
async: true,
processData: false,
cache: false,
success: function (data) {
//var obj = JSON.parse(data)
// alert(data);
$('#Name').val(data.MobileSerial);
$('#myModal').modal('show');
},
error: function (errormessage) {
alert(errormessage.responseText);
}
});
}
I'm calling php file through ajax call and if it returns nothing i want to redirect user to another page (It's for error reports, if it doesn't return anything it means that user logged in). Tried to add error section but it doesn't work. Any suggestions will help. Thanks! Btw, I have small jQuery function at the top of the ajax function, why it breaks my whole ajax call?
ajax.js
function loginAjax() {
//$("#email_errors").empty(); //This function doesnt work and kills whole ajax call. Here is loginAjax function call line - <button type = "submit" id = "push_button" onclick = "loginAjax(); return false">PushMe</button>
$.ajax({
url: "Classes/call_methods_login.php",
type: "POST",
dataType: "json",
data: {
login_email: $("#login_email").val(),
login_password: $("#login_password").val(),
},
success: function(data) {
$("#login_error").html(data.login_message);
}
});
}
$.ajax({
url: "Classes/call_methods_login.php",
type: "POST",
dataType: "json",
data: {
login_email: $("#login_email").val(),
login_password: $("#login_password").val(),
},
success: function(data) {
$("#login_error").html(data.login_message);
},
error: function(){
window.location.replace("http://stackoverflow.com");
}
});
}
To redirect using javascript all you need to do is override the location.href attribute.
function loginAjax() {
$.ajax({
url: "Classes/call_methods_login.php",
type: "POST",
dataType: "json",
data: {
login_email: $("#login_email").val(),
login_password: $("#login_password").val(),
},
// the success method is deprecated in favor of done.
done: function(data) {
$("#login_error").html(data.login_message);
},
fail: function(data) {
location.href="path/to/error/page";
}
});
}
I am making ajax call to login with spring security but it shows username and password in the url no matter what.
ex: /?j_username=s&j_password=s
I am trying to find my mistake for a long time but I couldnt be able to see it. It is probably a small mistake.
here is my ajax call;
function performLogin() {
var j_username = $("#j_username").val();
var j_password = $("#j_password").val();
$.ajax({
cache: false,
type: 'POST',
url: "/login",
crossDomain: true,
async: false,
data: { 'j_username': j_username, 'j_password': j_password},
dataType: 'json',
beforeSend: function (xhr) {
xhr.setRequestHeader("x-ajax-call", "no-cache");
}
});
}
Thanks
EDIT:
It is resolved by adding `return false;`
But I am not sure if my approach is good. Here is the update;
'function performLogin() {
var j_username = $("#j_username").val();
var j_password = $("#j_password").val();
$.ajax({
cache: false,
type: 'POST',
url: "/Mojoping2/login",
crossDomain: true,
async: false,
data: { 'j_username': j_username, 'j_password': j_password},
dataType: 'json',
beforeSend: function (xhr) {
xhr.setRequestHeader("x-ajax-call", "no-cache");
},
success: window.location.reload()
});
return false;
}
It has nothing to do with the Ajax call. You are not cancelling the form submission!
function performLogin() {
var j_username = $("#j_username").val();
var j_password = $("#j_password").val();
$.ajax({
cache: false,
type: 'POST',
url: "/login",
crossDomain: true,
async: false,
data: { 'j_username': j_username, 'j_password': j_password},
dataType: 'json',
beforeSend: function (xhr) {
xhr.setRequestHeader("x-ajax-call", "no-cache");
}
});
return false;
}
and however you are adding the event
onsubmit="return performLogin();
If you are using jQuery to attach the event, you can use
function performLogin(evt) {
evt.preventDefault();
...
I created a jsfiddle to test your code and have been unable to replicate your bug. The POST request appears to submit normally (going via the chrome dev tools), and no extra GET values are appearing for me.
Demo: http://jsfiddle.net/5EXWT/1/
Out of interest, what version of jQuery are you using?
{this is just here so i can submit the jsfiddle}
I'm trying to show a div with gif animation inside when a button click via javascript call. But the problem is, it's working fine in firefox only. In IE and Chrome, the animation won't show up. I've tried debugging with alert("stop"); before and after the animation and it's indeed working, but after I removed the alert, it won't work anymore. Any suggestion please?
Here's the snippet for the TestPage.aspx:
<div id="animationDiv" style="display: none;">
<img src="loading.gif" />
</div>
...
<div>
<asp:Button ID="testButton" runat="server" Text="Test AJAX" OnClientClick="return testButtonClick();" />
</div>
...
<script type="text/javascript">
<!--
var docWidth, docHeight;
function testButtonClick() {
var o_animationDiv = $("#animationDiv");
o_animationDiv.width(docWidth);
o_animationDiv.height(docHeight);
o_animationDiv.show();
$.ajax({
type: "Post",
url: "TestPage.aspx/TestAjax",
async: false,
cache: false,
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: testAjaxSuccess,
error: testAjaxError
});
function testAjaxSuccess(data, status, xhr) {
// do something here
};
function testAjaxError(xhr, status, error) {
// do something here
};
o_animationDiv.hide();
return false;
};
$(document).ready(function () {
docWidth = $(document).width();
docHeight = $(document).height();
});
//-->
</script>
Here's the snippet for TestPage.aspx.cs:
// using PageMethod for ajax
[System.Web.Services.WebMethod(EnableSession = true)]
public static string TestAjax()
{
System.Threading.Thread.Sleep(5000); // 5 seconds
// or do something else here
// ie. if validation error, throw an exception
return string.Empty;
}
UPDATE 1: added some javascript function
Why don't you call the .hide() on the post success:
$.ajax({
type : "Post",
url : "TestPage.aspx/TestAjax",
cache : false,
data : "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
// hide element when POST return as arrived,
// meaning the "5 seconds" period has ended!
o_animationDiv.hide();
}
});
You can read about callback function at the jQuery documentation!
jQuery ajax is asynchronous, so that won't wait for ajax result to hide again.
o_animationDiv.hide();
o_animationDiv.show();
This lines work in serial and you can't see if it's working properly. So you should wait for ajax result to hide it.Try this,
$.ajax({
type: "Post",
url: "TestPage.aspx/TestAjax",
async: false,
cache: false,
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json"
}).done(function() {
o_animationDiv.hide();
});
UPDATE:
Sorry I missed the point that you create a synchronous ajax request.
But there is a problem already. Most browsers lock itself when running a synchronous XHR. This also effects previous proccess sometimes.
Here is a similar question
Maybe if you use beforeSend, you can avoid this problem.
beforeSend
A pre-request callback function that can be used to modify the jqXHR
(in jQuery 1.4.x, XMLHTTPRequest) object before it is sent
Try this one,
$.ajax({
type: "Post",
url: "TestPage.aspx/TestAjax",
async: false,
cache: false,
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
beforeSend : function() {
o_animationDiv.show();
}
}).done(function() {
o_animationDiv.hide();
});
UPDATE-2:
If that doesn't work, try this one,
...
o_animationDiv.width(docWidth);
o_animationDiv.height(docHeight);
o_animationDiv.show();
setTimeout(function() {
$.ajax({
type: "Post",
url: "TestPage.aspx/TestAjax",
async: false,
cache: false,
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: testAjaxSuccess,
error: testAjaxError
}).done(function() {
o_animationDiv.hide();
});
}, 600);