Can someone please help me out on how to create a hyperlink for the output from append(response).
<script type="text/javascript">
$(document).ready(function () {
$("#Sid").change(function () {
if (document.getElementById("Sid").selectedIndex == 0) {
$("#Did").empty();
}
else {
var SitId = $(this).val();
debugger
$.ajax({
type: "post",
url: "/Dropdown/GetDeviceList?Sid=" + SitId,
contentType: "html",
success: function (response) {
debugger
$("#Did").empty();
$("#Did").append(response);
If the response is a url string :
$("#Did").append(`link text`)
Related
I got a scenario:
Here's my ActionResult when ActionLink was Clicked
public PartialViewResult ViewHit(string id, string recordid,string statusSelect,string statusHitSelect)
{
//some code here....
}
The ActionLink where to post after the Button was clicked:
public ActionResult SaveUpdate(String statusSelect, string statusHitSelect)
{
return PartialView("ViewHitList");
}
Here's the button:
<input type="button" value="Save" id="savehit"/>
and Here's my Ajax:
$("#savehit").on('click', function () {
//alert("Button was click!");
$.ajax({
type: "post",
contentType: "application/json; charset=utf-8",
//url: "SelectUpdate?statusSelect=" + statusSelect + '&statusHitSelect=' + statusHitSelect,
url: "SaveUpdate",
data: "{'statusSelect':'" + statusSelect + "','statusHitSelect':'" + statusHitSelect + "'}",
//data:null,
success: function (response) {
if (response != null && response.success) {
//InformationMessageSuccess(response.responseText);
alert("success");
} else {
// DoSomethingElse()
//InformationMessageFailed(response.responseText);
alert("not success");
}
},
});
});
The problem is, when i hit the save button using debug mode the ActionResult called was the ViewHit instead of SaveUpdate.
I am wondering why is it happen?
Any great idea is highly appreciated.
You can try to avoid default action of the event by using event.preventDefault()
$("#savehit").on('click', function (event) {
event.preventDefault();
//alert("Button was click!");
$.ajax({
type: "post",
contentType: "application/json; charset=utf-8",
//url: "SelectUpdate?statusSelect=" + statusSelect + '&statusHitSelect=' + statusHitSelect,
url: "#Url.Action("SaveUpdate", "Home")",
data: "{'statusSelect':'" + statusSelect + "','statusHitSelect':'" + statusHitSelect + "'}",
//data:null,
success: function (response) {
if (response != null && response.success) {
//InformationMessageSuccess(response.responseText);
alert("success");
} else {
// DoSomethingElse()
//InformationMessageFailed(response.responseText);
alert("not success");
}
}
});
});
Add the [HttpPost] attribute to the SaveUpdate action
In the ajax, change the URL to url: "ControllerName/SaveUpdate"
Since you are expecting a boolean result (JSON) back to your page as the ajax response, simply return a jsonResult (true/false)
SaveUpdate is not post type please add attribute [HttpPost] at the top of the controller method like
[HttpPost]
public ActionResult SaveUpdate(String statusSelect, string statusHitSelect)
{
return PartialView("ViewHitList");
}
I have changed the ajax call like bellow and it the SaveUpdate method. Bellow is the ajax call.
$(document).ready(function () {
$("#savehit").on('click', function () {
var statusSelectData="statusSelect";
var statusHitSelectData="statusHitSelect";
var postData = {
statusSelect: statusSelectData,
statusHitSelect: statusHitSelectData
}
//alert("Button was click!");
$.ajax({
type: "POST",
url: "/Home/SaveUpdate",
data: postData,
//data:null,
success: function (response) {
if (response != null && response.success) {
//InformationMessageSuccess(response.responseText);
alert("success");
} else {
// DoSomethingElse()
//InformationMessageFailed(response.responseText);
alert("not success");
}
},
});
});
});
The code is:
$.ajax({
type: "POST",
url: '#Url.Action("getFilterActiveData")',
dataType: "json",
mtype: "post",
traditional: true,
data: {
values: arg
},
async: true,
beforeSend: function () {
$("#filter tr").live('click', function () {
document.getElementById('filter_btn').style.pointerEvents = 'none';
});
$('#filter_loading').fadeIn();
},
success: function (data) {
$('#filter_loading').fadeOut();
$("#filter tr").live('click', function () {
alert("does not shown");
if (isSection == false) {
$('#filter_btn').click().promise().done(function () {
document.getElementById('filter_btn').style.pointerEvents = 'auto';
});
}
});
}
});
The alert is not shown, but if I write out side of the AJAX directly like:
$("#filter tr").click(function () {
alert("clicked " + isSection);
Then it will show. Any suggestions please? I can make a function that will be called in success but I don't know about what to do in beforeSend()
Try this one , live method is depreciated long ago. Instead of live you can use the $.on() , $.bind() or something like that..
$.ajax({
type: "POST",
url: 'file url',
dataType: "json",
method : "post",
data: { values: arg },
async: true,
beforeSend: function(){
$("#filter tr").on('click', function () {
document.getElementById('filter_btn').style.pointerEvents = 'none';
});
$('#filter_loading').fadeIn();
},
success: function (data) {
$('#filter_loading').fadeOut();
$("#filter tr").click(function () {
alert("does not shown");
if (isSection == false) {
$('#filter_btn').click().promise().done(function () {
document.getElementById('filter_btn').style.pointerEvents = 'auto';
}
});
}
});
Here is my ajax call
$(document).ready(function () {
$("#btnSubmit").click(function () {
alert("I am in ?");
$.ajax({
type: "POST",
url: "TestNew2.aspx/DisplayData",
data: "{}",
contentType: "application/x-www-form-urlencoded",
dataType: "text",
//success: function (msg) {
// // Replace the div's content with the page method's return.
// $("#btnSubmit").text(msg.d);
// alert(msg.d);
//}
success: function (result, status, xhr) {
document.getElementById("lblOutput").innerHTML = xhr.responseText
},
error: function (xhr, status, error) {
alert(xhr.error);
}
});
});
});
and my Web method[WebMethod]
public static string DisplayData()
{
return DateTime.Now.ToString();
}
Getting aspx page when trying to call web method on aspx page.Here is the jQuery code
Can any one point out what may be wrong.Because the web method is not getting called.
Try Like
$.ajax
({
url: " URL",
data: "{ 'name' : 'DATA'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
async: true,
dataFilter: function (data) { return data; },
success: function (data)
{
alert(data);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("error");
}
});
OR
jQuery.ajax({
type: "POST",
url: "Login.aspx/checkUserNameAvail",
contentType: "application/json; charset=utf-8",
data: "{'iuser':'" + userid + "'}",
dataType: "xml",
success: function (msg) {
$(msg).find("Table").each(function () {
var username = $(this).find('UserName').text();
if (username != '') {
//window.location.replace('/iCalendar.aspx');
alert('This username already taken..');
$("#reguser").val('');
$("#reguser").focus();
}
else {
}
});
},
error: function (d) {
}
});
.CS
[WebMethod(enableSession: true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public static string checkUserNameAvail(string iuser)
{
try
{
iCalendarClass iC = new iCalendarClass();
DataSet ds = iC.checkUserNameAvail(iuser);
return (ds.GetXml());
}
catch
{
return null;
}
}
I am using ajax to call a [WebMethod].But it doesn't Works.Code i used is
ASPX page
$.ajax({
type: "POST",
url: '<%= ResolveUrl("~/CandidateManagement.aspx/GetCurrentDateTime") %>',
data: '{name: "' + document.getElementById("lbcb5").innerHTML + '" }', Value
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
Aspx.cs page
[WebMethod]
public static string GetCurrentDateTime(string name)
{
return "Hey! " + name + Environment.NewLine + "The Current Date & Time is: "
+ DateTime.Now.ToString();
}
Thanks in advance
you don't need quotes on data, do like this:
$.ajax({
type: "POST",
url: '<%= ResolveUrl("~/CandidateManagement.aspx/GetCurrentDateTime") %>',
data: {name: document.getElementById("lbcb5").innerHTML},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response)
{
alert(response)
},
failure: function (response) {
alert(response.d);
}
});
Did you add a ScriptManager in you aspx page?
I think you should try to add it with the attribute EnablePageMethods="true"
try this:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
"When i use ScriptManager shows an error Only one instance of a ScriptManager can be added to the page."
when this type of error is thrown, then use scriptmanager proxy
<ajax:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
</ajax:ScriptManagerProxy>
--------------************DEFAULT.ASPX************--------------
<script type="text/javascript">
function YolAl(yol) {
deger='<%=ResolveUrl("~/'+ yol +'") %>'
return deger;
}
</script>
*******************------USERCONTROL CALL JS-------************************
$(window).scroll(function () {
if ($(window).scrollTop() == $(document).height() - $(window).height()) {
deger += 1;
RepeaterFnc("BaglantiItem.ascx", deger, "BagListDiv", YolAl('Default.aspx/AddGadGets'));
}
});
$(document).ready(function () {
deger += 1;
RepeaterFnc("DurumGuncelle.ascx", deger, "BagListDiv", YolAl('Default.aspx/AddGadGets'));
RepeaterFnc("IlanlarTab.ascx", deger, "SolDivAlanId", YolAl('Default.aspx/AddGadGets'));
RepeaterFnc("BaglantiItem.ascx", deger, "BagListDiv", YolAl('Default.aspx/AddGadGets'));
RepeaterFnc("Top10TakipEt.ascx", deger, "SagDivFavoriId", YolAl('Default.aspx/AddGadGets'));
RepeaterFnc("Top10Ilanlar.ascx", deger, "SolDivAlanId", YolAl('Default.aspx/AddGadGets'));
});
function TakipEtYenileFnc() {
$('#SagDivFavoriId').html('');
RepeaterFnc("Top10TakipEt.ascx", deger, "SagDivFavoriId", YolAl('Default.aspx/AddGadGets'));
}
function RepeaterFnc(usercontrol, deger, DivName,FuncName) {
$('#ajaxloading').show();
var NextId = deger;
$.ajax({
url: FuncName,
type: "POST",
data: "{usercontrol:'" + usercontrol + "', NextId:'" + NextId + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
OnSuccess1(msg, DivName);
},
error: function (msg) { OnError1(DivName); }
});
deger = parseInt(deger + 1);
}
function OnSuccess1(data, DivName) {
$("#" + DivName).html($("#" + DivName).html() + data.d);
$('#ajaxloading').hide();
}
function OnError1(divname) {
return false;
$("#" + divname).html("Error!");
}
I have this code in my controller:
int number = 0;
public JsonResult JsonData()
{
for (int i = 0; i < 21; i++)
{
number += i;
return Json(number, JsonRequestBehavior.AllowGet);
}
return Json(number, JsonRequestBehavior.AllowGet);
}
And loop with jQuery Ajax script in the view:
<script type="text/javascript">
var tag = true;
$(document).ready(function () {
while (tag) {
$.ajax({
url: "#Url.Action("JsonData", "Home")",
dataType: 'json',
success: function (data) {
console.log(data);
$('#counter').html('');
$('#counter').html(data);
if (data == 20) {
tag = false;
}
}
});
}
});
</script>
<div id="counter"></div>
My goal is to make jQuery Ajax to constantly get "number" from action "JasonData" and print that number to "DIV tag" But my current code is not working for some reason, - the result is that browser window will not stop loading if i'm using "while()" loop, if I'm not using "while()" loop it only prints the first value of "number" variable.
Use a setInterval/setTimeout instead of while loop, because continuously running script will block the UI and you can utilize the callback mechanism instead.
$(document).ready(function () {
var interval = window.setInterval(function(){
$.ajax({
url: "#Url.Action("JsonData", "Home")",
dataType: 'json',
success: function (data) {
$('#counter').html(data);
if (data == 20) { //<-- make sure data has value 20 and is not an object or anything
window.clearInterval(interval);
}
}
});
}, 1000); //<-- Give interval in milliseconds here
});
Demo
Or with timeout:
$(document).ready(function () {
makeAjaxCall();
});
function makeAjaxCall(){
$.ajax({
url: "#Url.Action("JsonData", "Home")",
dataType: 'json',
success: function (data) {
$('#counter').html(data);
if (+data < 20) {
window.setTimeout(makeAjaxCall, 1000); //Duration in ms
}
}
});
}
Demo