I want Delete row with jquery ajax , in my view i call js function for ajax controller/action
<a href="javascript:void(0);" onClick="markup2('@Url.Action("ChannelDelete", "MyInfo", new {area = ""})');" class="btn btn-lg btn-danger" style="margin-bottom: 15px;">Delete</a>
function markup2(e) {
$(function() {
$("#LoadPages").empty();
$("#LoadPages").html('<div style="padding: 20px 15px"><div class="row"><div class="center-block col-md-1" style="float: none;"><img src="/Upload/pageLoader.gif" alt="loading..." /></div></div></div>');
$.ajax({
type: "POST",
url: e
}).done(function(data) {
$("#LoadPages").load(data);
});
return false;
});
}
The above function it's ok too , get url and send AJAX POST and get data;
when return data (html) and Inserting into the $("#LoadPages") element (("#LoadPages").load(data))
get me an error:
Error: Syntax error, unrecognized expression:
<h2>my channels</h2> <table class="table"> <tr> <th> channel name </th> <th> name </th> <th> UserSelectedChannelJoinDate </th> <th></th> </tr> </table>
I don't know why get error
Firstly, you should really use unobtrusive event handlers instead of outdated on* event attributes.
Secondly, your issue is due to the use of load(). That method is intended to receive a URL to make an AJAX request which returns HTML, which is then set as the content of the specified element. However your code is giving a HTML string instead of a URL, hence the error.
To fix this, use html() instead of load():
<a href="#" data-action="@Url.Action("ChannelDelete", "MyInfo", new {area = "" })" class="btn btn-lg btn-danger">Delete</a>
$(function() {
$('.btn').click(function(e) {
e.preventDefault();
$("#LoadPages").html('<div style="padding: 20px 15px"><div class="row"><div class="center-block col-md-1" style="float: none;"><img src="/Upload/pageLoader.gif" alt="loading..." /></div></div></div>');
$.ajax({
type: "POST",
url: $(this).data('action')
}).done(function(data) {
$("#LoadPages").html(data);
});
});
});
I would also strongly suggest you use an external stylesheet instead of the style attribute in your HTML.