I am trying to use the delete function to delete a track from the user's soundcloud account while also deleting the track from my database. It is successfully deleting the information from my database, but is not deleting from the soundcloud's database and I really cannot understand why! No errors show up until the end, but just does not delete the track from soundcloud.
This is my code I am using:
$(".deleteTrack").live("click", function () {
if (SC.isConnected) {
var scTid = $(this).attr('id');
var path = "/tracks/" + scTid;
var con = confirm("Are you sure you want to delete this track?");
if (con) {
$.ajax({
type: "POST",
contentType: "application/json",
url: "JamWithInI.aspx/GetTrackInfo",
data: "{'scTid':'" + scTid + "'}",
dataType: "json",
success: function(str){
inst = str.d["1"];
SC.delete(path, function(){
$.ajax({
type: "POST",
contentType: "application/json",
url: "JamWithInI.aspx/DeleteTrack",
data: "{'scTid':'" + scTid + "'}",
dataType: "json",
success: function(){
alert("Your track has been deleted");
$("#openInstruments").trigger('click').trigger('click');
$(".instrument").trigger('click').bind('click').trigger('click');
inst = null;
},
error: function () {
alert("Track did not delete succesfully");
}
});
});
},
error: function () {
alert("An error occurred");
}
});
}
}
});
The SC.delete() method calls your callback with a response object and error (if any). Add something like the following to find out if things are working properly:
SC.delete(path, function(response, error) {
alert(response.status);
if (error) {
alert(error);
}
});
If your delete request was successful, you should get a single alert with the text '200 - OK'. If not, the error message will be informative.
Related
My problem is that when I press yes on delete confirmation the db record goes away, but if I refresh the page it comes back.
Maybe I have to put the language and the id inside the Ajax URL?
If yes how can I do this?
Please forgive me I am still learning.
This is for java eclipse and SQL database
var assetId;
var batchId;
function cancelOpname(){
$.ajax({
type: "POST",
cache: false,
url: urlPage + "opname/cancel",
dataType: "JSON",
data: data,
beforeSend: function() {
$('.loader').fadeIn();
},
complete: function() {
$('.loader').fadeOut();
},
success: function() {
if (data.errorResult == false) {
console.log("Success: else ");
javascript:window.history.back();
} else {
$('.loader').fadeOut();
}
},
error : function(e) {
console.log("ERROR: ", e);
}
});
}
no error but ajax and jQuery not working
I'm new in MVC and trying to create a site for training.
I need to call a POST method in my controller through JavaScript.
This is my JavaScript method:
function ActivateAddStarAndRoleWithCategories(sender, args) {
var discID = $('#DiscID').val();
var starID = $('#StarID').val();
var desc = $("#Description").val();
var strID = "";
$(':checkbox').each(function () {
strID += this.checked ? this.id + "," : "";
});
strID = strID.substr(0, strID.length - 1);
$.ajax({
type: "POST",
url: 'CreateCategoriesID',
contentType: "application/json; charset=utf-8",
data: {
discID: discID,
starID: starID,
description: desc,
catID: strID
},
dataType: "json",
success: function () { alert("success!"); },
error: function () { alert("Fail!"); }
});
}
And this is my method in the controller:
[HttpPost]
public ActionResult CreateCategoriesID(string discID, string starID, string description, string catID)
{
entities.AddStarAndRoleAndCategories(int.Parse(starID), description, int.Parse(discID), catID);
return Json("OK", JsonRequestBehavior.AllowGet);
}
The view that activates the JavaScript function is already connected to the controller.
I always get error 500 - the server responded with a status of 500.
What am I doing wrong?
Thank you in advance!
I would go with the examples from the jQuery docs.
Did you try something like...?
$.ajax({
method: "POST",
url: "CreateCategoriesID", // Insert your full URL here, that COULD be the problem too
// This contentType is the default, so we can just ignore it
//contentType: "application/x-www-form-urlencoded; charset=UTF-8",
// I would also ignore the dataType unless your backend explicitly sends a JSON response header
//dataType: "json",
data: {
discID: discID,
starID: starID,
description: desc,
catID: strID
}
}).done(function(msg) {
alert('Success with message: ' + msg);
}).fail(function(jqXHR, textStatus) {
alert('Failed with status: ' + textStatus);
});
I am using knockout with MVC in my project. when I pass viewModel on dropdown change It's getting continues ajax request to the server. How can I avoid the continues request??? any one can please help me on this???
My View
#ko.Html.DropDownList(m => m.RoomList, new { #class = "full-width", #id = "rmch" }, "Text", "Value").Value(m=>m.NoOfRooms)
Javascript
$(document).ready(function () {
$('#rmch').on("change", function (e) {
//viewModel.NoOfRooms = $(this).val();
$.ajax({
url: '#Url.Action("DropChange", "Home")',
type: 'POST',
data: ko.mapping.toJSON(viewModel),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
if (data.redirect) {
location.href = resolveUrl(data.url);
}
else {
//ko.applyBindings(viewModel, document.getElementById("p_scentsFH"));
ko.mapping.fromJS(data, viewModel);
}
},
error: function (error) {
alert("There was an error posting the data to the server: " + error.responseText);
},
})
});
})
if I remove value part from the dropdownlist in the view It's working .but I need the value for the processing.
The simplest thing I can think of is to set the dropdownlist to readonly before updating it, then when you have finished adding the new items, remove the readonly attribute.
$(document).ready(function () {
$('#rmch').on("change", function (e) {
//viewModel.NoOfRooms = $(this).val();
$.ajax({
url: '#Url.Action("DropChange", "Home")',
type: 'POST',
data: ko.mapping.toJSON(viewModel),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
//this will disable the onchange event
$('#rmch').attr("readonly","readonly");
if (data.redirect) {
location.href = resolveUrl(data.url);
}
else {
//ko.applyBindings(viewModel, document.getElementById("p_scentsFH"));
ko.mapping.fromJS(data, viewModel);
}
//this will enable the onchange event for the next time you select something.
$('#rmch').removeAttr("readonly");
},
error: function (error) {
alert("There was an error posting the data to the server: " + error.responseText);
},
})
});
})
The following script is failing and I have no idea why. Can anyone see anything glaring? I just used and alert to make sure that the userID and the postID were correct and they were so I know the call to the values is correct and I have the processing page in the same directory just to get it to work for now.
$(document).ready(function() {
$('#saveme').click(function() {
$.ajax({
type: "POST",
url: "save_recipe.php",
contentType: "application/json; charset=utf-8",
data: { 'postID': $('#post_id').val(), 'userID': $('#user_id').val() },
dataType: "json",
success: function (msg) {
alert("This recipe has been saved in your profile area!");
},
error: function (request, status, error) {
alert(request.responseText);
},
//alert('PostID: '+ $('#post_id').val() +' UserID: '+$('#user_id').val());
});
});
I have a javascript function which makes a JSON call to a web service using jQuery.
In the success function I need to evaluate the JSON response and if necessary make another call to a different method in the same web service.
Here is how I do it:
function firstAjaxCall(aid, tid) {
$.ajax({
type: "POST",
contentType: "application/json;charset=utf-8",
url: "/webservices/Webservice.asmx/Method1",
data: "{ auctionId: '" + aid + "'}",
dataType: "json",
success: function (response) {
var respData = response.d;
//do some stuff
if (respData.HasEnded == true) {
clearInterval(tid);
var end = false;
end = endFunction(aid);
if (end) {
// do some other stuff
}
}
},
failure: function (errorMsg) { alert(errorMsg.toString()); }
});
}
and the endFunction which is being called from within the ajax success function:
function endFunction(aid) {
var hasEnded = false;
$.ajax({
type: "POST",
contentType: "application/json;charset=utf-8",
url: "/webservices/Webservice.asmx/Method2",
data: "{ auctionId: '" + aid + "'}",
dataType: "json",
success: function (callbackData) {
var endRespData = callbackData.d;
hasEnded = endRespData.Success;
alert(hasEnded.toString());
},
failure: function(XMLHttpRequest, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
return hasEnded;
}
Here is the weird stuff. The ajax-call is made all right. The code on the server is running according to plan. However, if I try to set a firebug breakpoint in the success function of endFunction(aid) is is not hit, but the alert box is shown displaying the word true. This is somewhat good since it seems that we are actually reaching the success function. The hasEnded variable however is never set to true so it always returns false.
Calling endFunction(1) from the Firebug console displays an alert box with the word true and returns value false.
What's going wrong?
AJAX is asynchronous — the $.ajax call will not wait for the server to reply.
Therefore, the return hasEnded; line runs before the AJAX callback.
You need to make your endFunction take a callback parameter, like $.ajax does.
http://api.jquery.com/jQuery.ajax/
It looks like you're using "failure" in the documentation you have "error":
error(XMLHttpRequest, textStatus, errorThrown)
also you should do something like this:
function firstAjaxCall(aid, tid) {
$.ajax({
type: "POST",
contentType: "application/json;charset=utf-8",
url: "/webservices/Webservice.asmx/Method1",
data: "{ auctionId: '" + aid + "'}",
dataType: "json",
success: function (response) {
var respData = response.d;
//do some stuff
if (respData.HasEnded == true) {
clearInterval(tid);
var end = false;
endFunction(aid,function(endv){
if (endv) {
// do some other stuff
}
});
}
},
error: function (errorMsg) { alert(errorMsg.toString()); }
});
}
function endFunction(aid,endcallback) {
var hasEnded = false;
$.ajax({
type: "POST",
contentType: "application/json;charset=utf-8",
url: "/webservices/Webservice.asmx/Method2",
data: "{ auctionId: '" + aid + "'}",
dataType: "json",
success: function (callbackData) {
var endRespData = callbackData.d;
hasEnded = endRespData.Success;
alert(hasEnded.toString());
endcallback(hasEnded);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
endcallback("?");
}
});
//return hasEnded;
}