When I call the snippet below I get blank content instead of the code saved in the snippet. Can someone please tell me what I'm missing? If I replace the code with a string like "foobar" the string displays when I call the snippet. So it seems I'm missing something.
<snippet>
<content>
<![CDATA[
$.ajax({
url: '/path/to/file',
type: 'POST',
dataType: 'xml/html/script/json/jsonp',
data: {param1: 'value1'},
complete: function(xhr, textStatus) {
//called when complete
},
success: function(data, textStatus, xhr) {
//called when successful
},
error: function(xhr, textStatus, errorThrown) {
//called when there is an error
}
});
]]>
</content>
<tabTrigger>ajax</tabTrigger>
</snippet>
Just escape your jquery call
<snippet>
<content>
<![CDATA[
\$.ajax({
url: '/path/to/file',
type: 'POST',
dataType: 'xml/html/script/json/jsonp',
data: {param1: 'value1'},
complete: function(xhr, textStatus) {
//called when complete
},
success: function(data, textStatus, xhr) {
//called when successful
},
error: function(xhr, textStatus, errorThrown) {
//called when there is an error
}
});
]]>
</content>
<tabTrigger>ajax</tabTrigger>
</snippet>
Related
Can anyone see why this script would fail to redirect?
$.ajax({
url: url,
data: JSON.stringify(viewModel),
dataType: 'json',
contentType: 'application/json; charset=utf-8',
cache: false,
type: "POST",
success: function (result) {
if (result.success === true) {
showFullScreenLoadMask(false);
//var redirect = "/Login/Index";
//window.location.href = getUrlPath() + redirect;
var redirecturl = getUrlPath() + "/Login/Index";
/*F5_*/ F5_Deflate_location( /*_5F##*/ window /*F5_*/ ) /*_5F#.location#*/ .assign = redirecturl;
} else {
// hide any previous errors
$("#divPasswordHistoryError").hide();
$("#divFullNameError").hide();
$("#divCurrentPasswordError").hide();
if (result.vm.ShowPreviousPasswordFail === true) {
$("#divPasswordHistoryError").show();
$("#divPasswordHistoryError").removeClass("hidden");
}
if (result.vm.ShowFullNameError === true) {
$("#divFullNameError").show();
$("#divFullNameError").removeClass("hidden");
}
if (result.vm.ShowCurrentPasswordFail === true) {
$("#divCurrentPasswordError").show();
$("#divCurrentPasswordError").removeClass("hidden");
}
$("#Username").val(result.vm.Username);
$("#Password").val(result.vm.Password);
$("#NewPassword").val("");
}
},
error: function (responseText, textStatus, errorThrown) {
alert('Error - ' + errorThrown);
}
});
Above is the code I have on my login script, but for the life of me, I cant get my head around as to why it would not be redirecting to the correct page after someone enters their password.
It uploads the image but it redirects to UploadImage.cshtml. So apparently ev.preventDefault isn't working. Is there a solution to this without having me to go through blueimp or xhtml stuffs coz I am not ready to touch those stuffs.
HTML
<form method="post" action="UploadImage.cshtml" id="imgUploadForm" class="imgUpload" enctype="multipart/form-data">
#FileUpload.GetHtml(
initialNumberOfFiles:1,
allowMoreFilesToBeAdded:false,
includeFormTag: false,
uploadText:"Upload")
<input type="submit" name="buttonUpload2" value="Upload" />
</form>
Jquery
$(function () {
$('.imgUpload').submit(function (ev) {
var frm = $(this);
Upload(frm);
ev.preventDefault();
});
});
function Upload(frm) {
$.ajax({
type: frm.attr('method'),
url: frm.attr('action'),
data: new FormData(frm),
success: function (data) {
alert('Success');
},
error: function () {
alert('Error');
}
});
}
Solved with following Jquery code. Thanks
$(document).ready(function (e) {
$('.imgUpload').on('submit', (function (e) {
e.preventDefault();
var formData = new FormData(this);
$.ajax({
type: 'POST',
url: $(this).attr('action'),
data: formData,
cache: false,
contentType: false,
processData: false,
success: function (data) {
alert('success');
},
error: function (data) {
alert('error');
}
});
}));
})
OK new at this / long time away from it...
I have a json list created thus:
$.ajax({
url: 'http://www.somewebsite.com/land.php',
dataType: 'jsonp',
jsonp: 'jsoncallback',
timeout: 5000,
success: function(data, status) {
$.each(data, function(i, item) {
var balance = '<p>' + item.itemm + ' ' + item.amount + ' ' + item.status +
' ' + 'edit' + ' ' +
' delete' +
'</p><hr>';
total2 = total2 + parseInt(item.amount);
$("#mylbl").append(balance);
$("#mylbl5").html(total2);
});
},
error: function() {
output.text('There was an error loading the data.');
}
I'm using this method as I'm using phonegap...I have display items and add items working but want to add in edit and delete functionality...
I'm trying to add edit and delete links on each row (as above) but just cant get it sorted....
Only at delete stage .. and have tried this delete function..
function deleteRecord(id) // Get id of record . Function Call when Delete Button Click..
{
mysql_connect("localhost","454547_yeah","password");
mysql_select_db("454487_mydbdb");
var deleteStatement = mysql_query("DELETE FROM balance WHERE id=?");
db.transaction(function (tx) { tx.executeSql(deleteStatement, id, onError); alert("Delete Sucessfully"); });
}
Any help or pointers greatly appreciated ....I've trawlled the web and found nought that might help...
you can try this one
formDataEdit = {
itemm: $("#itemm"),
amount: $("#amount"),
status: $("#status")
};
$.ajax({
url: 'http://www.somewebsite.com/delete.php',
dataType: 'jsonp',
jsonp: 'jsoncallback',
timeout: 5000,
data: formDataEdit,
success: function(data, status) {
alert("Edit successful");
},
error: function() {
output.text('There was an error loading the data.');
}
});
formDataDelete = {
id: $("#id")
};
$.ajax({
url: 'http://www.somewebsite.com/delete.php',
dataType: 'jsonp',
jsonp: 'jsoncallback',
timeout: 5000,
data: formDataDelete,
success: function(data, status) {
alert("Delete successful");
},
error: function() {
output.text('There was an error loading the data.');
}
});
html
<b><a onclick="edit(1);">Edit</a> | <a onclick="delete(1);">Delete</a></b>
js
currentPage = {};
currentPage.edit = function(id){ //also can use for delete
alert(id);
};
Code below works fine in IE, but onsuccess(...) method is not getting called in Chrome. Does any one know the solution?
$(document).ready(
function (e) {
$.support.cors = true;
$.ajax({
dataType: "JSON",
type: "GET",
crossDomain: true,
url: "http://localhost:36730/home/GETaaa",
success: function (data) {
//console.log(data);
alert(data);
},
Error: function (xhr, error, message) {
alert(xhr);
}
});
}
)
I'm using jQuery 1.9.1 performing jsonp against the Etsy API. Below is a routine used to call into the API; it accepts a signed request (url), a jsonp callback name and an error handler.
call: function (signedOAuthRequest, callback, errorFn) {
$.ajax({
url: signedOAuthRequest,
type: "GET",
dataType: "jsonp",
jsonpCallback: callback,
jsonp: false,
crossDomain: true,
cache: true,
processData: false,
error: function (jqXHR, textStatus, errorThrown) {
if (errorFn != undefined) {
errorFn(jqXHR, textStatus, errorThrown);
}
}
});
Usage:
call("http://the.api/?signedrequest", "myCallback", myErrorHandler);
And it works. myCallback fires, the json object received looks good. Everything's fine.
Except...
Immediately after myCallback completes, the error handler runs. In the error handler, the status code is 200 and the error message is "parsererror". errorThrown.message is "myCallback was not called". Thing is, myCallback was definitely called and the json object passed to it was parsed just fine, so I don't get this at all.
Why is this error being thrown on an otherwise successful call?
Based on comments and what's in your question, this is how i would write that code:
function (signedOAuthRequest, callbackName, successFn, errorFn) {
$.ajax({
url: signedOAuthRequest,
type: "GET",
dataType: "jsonp",
jsonpCallback: callbackName,
jsonp: false,
cache: true,
success: successFn,
error: function (jqXHR, textStatus, errorThrown) {
if (typeof errorFn !== 'undefined') {
errorFn(jqXHR, textStatus, errorThrown);
}
}
});
}
I made the following changes:
Added an additional parameter to pass a success callback. This is required because jQuery will define the callback for you so that it can retrieve the successful response and parse it.
Removed crossdomain: true because it is redundant
Removed proccessData: false because it doesn't affect jsonp requests and you aren't passing any data
Added success: successFn so that jQuery will execute successFn on success with the parsed json data.
Renamed callback to callbackName so that it's more obvious about what it contains.
An alternate solution if you still insist on defining the callback yourself is to use the script dataType (taken from comments below)
$.ajax({
url: signedOAuthRequest,
dataType: "script",
cache: true
});