8
Answers

button should not postback if jquery.ajax return false

Susanta Rout

Susanta Rout

8y
322
1
i have a button i want that it should not be postback to serverside if jquery ajax featching value from database return false.if jquery ajax return true.button will postback to serverside code
Answers (8)
0
Susanta Rout
NA 767 52.1k 8y

Vasanti

sorry to say i dont want to use async:false. bcz its some perfomace problem
 
0
ravi verma
NA 81 411 8y
Try to use input type button not input type Submit.
0
Dipika
NA 1.2k 14.6k 8y
this works for me 
 
 
<script type="text/javascript" language="javascript">
$(document).ready(function () {
$("#<%=btnOK.ClientID%>").click(function () {
var test;
$.ajax({
type: "POST",
data: {},
async: false,
url: "WebService.asmx/HelloWorld",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
error: function (xhr, errorType, exception) {
try {
alert(xhr.StackTrace);
var responseText;
responseText = $.parseJSON(xhr.responseText);
var v = JSON.stringify(xhr.responseText);
$("#msg").html(v);
alert('error :' + v);
var d = xhr.stackTrace;
test = false;
}
catch (e) {
}
},
success: function (response) {
alert('sucess :' + response.d);
if (response.d == "Hello World") {
test = true;
$('#<%=btnOK.ClientID%>').submit();
alert('inside success,value :' + test);
test= true;
}
else {
test= false;
}
//alert("t- " + test); // here we can call server side code
}
});
//alert('outside, value:' + test);
return test;
});
});
</script>
0
Susanta Rout
NA 767 52.1k 8y
button id.submit() not work if it is sucess...its urent plz some buddy help me
0
Susanta Rout
NA 767 52.1k 8y
var test;
$.ajax({
type: "POST",
url: "WebService1.asmx/HelloWorld",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
error: function (xhr,errorType,exception)
{
try
{
alert(xhr.StackTrace);
var responseText;
responseText = $.parseJSON(xhr.responseText);
var v = JSON.stringify(xhr.responseText);
$("#msg").html(v);
alert('error :' + v);
var d = xhr.stackTrace;
}
catch(e)
{
}
return false;
},
success:function(response)
{
alert('sucess :' + response.d);
if(response.d=="Hello World")
{
test = true;
$('#<%=btnOK.ClientID%>').submit();
alert('inside success,value :' + test);
}
else {
test = false;
//return test;
}
// alert(test);// here we can call server side code
}
});
alert('outside, value:' + test);
return false;
});
0
Susanta Rout
NA 767 52.1k 8y

Vasanti

i already applied what you are suggesting about. 
0
Midhun T P
NA 19.7k 281.2k 8y
Hi,
 
Try below code -
 
$.ajax({
url : 'yoururl',
success : function(data) {
if (data == "true")
$('#yourbuttonid').submit();
else
return false;
}
}
});
return false; 
0
Dipika
NA 1.2k 14.6k 8y
write the jquery.ajax code on client side button click and then return true false accordingly