Jquery Annotate / PHP error by sending the object value - ajax

Send var annotation_id:
echo $json = json_encode(array('annotation_id'=>$save[0]));
Get var annotation_id:
if (image.useAjax) {
$.ajax({
url: image.saveUrl,
data: form.serialize(),
error: function(e) {
alert("An error occured saving that note.");
},
success: function(data) {
if (data.annotation_id != undefined) {
editable.note.id = data.annotation_id;
alert(editable.note.id);
}
},
dataType: "json"
});
}
So...what's wrong with my code ?
Not getting the value of this variable.

Related

Sending url using ajax on codeigniter

I have this Ajax script where I pass a link to the data variable link, but I
get a 412 error.
$(function() {
$(".check-multi").change(function() {
$.ajax({
type: "POST",
url: "<?php echo site_url('adm/updateproperty'); ?>",
async: true,
data: {
link: $(this).data('link')
},
success: function(msg) {
alert('Success');
if (msg != 'success') {
alert('Fail');
}
}
});
});
});
I have tried
link: encodeURI($(this).data('link'))
And
link: encodeURIComponent($(this).data('link'))
as is suggested on other threads but I still get the 412 error message.
Hope this will help you :
you have added newline character to json data that is why you got error
Do like this :
var link = $(this).data('link');
data: {"link" : link},
/*----OR do this -----*/
data: {"link" : $(this).data('link')},
instead of this :
data: {
link: $(this).data('link')
},
Whole code should be like this :
var link = $(this).data('link');
/*console.log(link)*/
$.ajax({
type: "POST",
url: "<?php echo site_url('adm/updateproperty'); ?>",
async: true,
data: {"link" : link },
success: function(msg) {
alert('Success');
if (msg != 'success') {
alert('Fail');
}
}
});
For More :https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/412
Please change your code in data options. Use this way
data: {"id": ID},
i.e. store the value in a variable and send that variable in data option. If we assume
ID=$(this).data('link');`
Then the code will be as follows:
$(function() {
$(".check-multi").change(function() {
$.ajax({
type: "POST",
url: "<?php echo site_url('adm/updateproperty'); ?>",
async: true,
data: {"id":ID},
success: function(msg) {
alert('Success');
if (msg != 'success') {
alert('Fail');
}
}
});
});
});
Please check it.

wrong ajax callback invoked using cordova

In my jqm app I make a POST using jQuery $.ajax sending and receiving json data. Everything is fine in the browser and on iPhone; on Android I noticed that when this server response is like this:
{ "code" : 500,
"errorMsg" : "bla bla bla",
"errors" : null,
"status" : "INTERNAL_SERVER_ERROR",
"success" : false
}
the ajax invokes the "error" callback and not the "success". This happens only on android and only if I include corova-2.0.0. js on the project. Any help?
I'm using cordova-2.0.0 with jqm 1.3.1 and jQuery 1.9.1
Here's my code:
var ajax = $.ajax({
type: "post",
url: url,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
data: data,
timeout: 30000
});
var success = function (d) {
if(d.success==true && obj.success)
obj.success(d.data);
else
{
var msg = parseErrors(d);
console.log(msg);
//open page passing results
obj.msgBus.fire("RequestResult", {
callback: function(){ obj.msgBus.fire("Welcome");},
success: false,
text: msg
});
}
};
var error = function (xhr, status, e) {
console.log('error ajax url:[' + url + '] status:[' + status + '] error:[' + e + ']');
if (obj.error) {
if ((typeof e == 'string'))
obj.error({
statusText: e,
code: xhr.statusCode,
text: xhr.responseText
});
else {
e = $.extend(e, {
statusText: status
});
obj.error(e);
}
}
};
var complete = function () {
if (obj.complete) {
obj.complete();
}
};
var parseErrors = function(d){
console.log( d);
if(d.errors==null){
return d.errorMsg;
}
else
{
var res="";
for (var i=0;i< d.errors.length;i++){
res+= "{0}: {1} <br/>".format( d.errors[i].field, d.errors[i].errorMsg);
}
return res;
}
};
ajax.success(success).error(error).complete(complete);
where data is an object like this:
{
"date" : 1371679200000,
"idBeach" : "1",
"idStuff" : 3,
"idUser" : "8",
"numStuff" : 1
}
Try this, it works for me
$.ajax({
async: false,
type: "POST",
url: "Your_URL",
dataType: "json",
success: function (data, textStatus, jqXHR) {
$.each(data, function (i, object) {
alert(obj.Data);
});
},
error: function () {
alert("There was an error loading the feed");
}
});

asp.net mvc 3 json does not work

This is my jquery with json
$('#btnVerificationOk').click(function () {
var verId = $('#trans_verification_id').val();
var verCode = $('#trans_verification_code').val();
$.ajax({
url: '/Profile/CompleteTransactions',
type: 'POST',
data: { },
dataType: 'json',
success: function (result) {
alert(result);
},
error: function () {
alert('ERROR ERROR !!!!!!!!!!!!');
}
});
});
And My C# method:
[Authorize]
[HttpPost]
private JsonResult CompleteTransactions()
{
return Json("Done");
}
Its always alerts 'ERROR ERROR !!!!!!!!!!!!' i tried debugging but CompleteTransactions method is not firing
And this is my second json which is bellow and works good
$('#btnTransfareOk').click(function () {
var userName = $('#transfare_username').val();
var amount = $('#transfare_amount').val();
if (userName == '' || amount == '') {
$('#transfare_error_list').html('Please fill boxes.');
} else {
$.ajax({
url: '/Profile/TranfareMoney',
type: 'POST',
data: { ToUsername: userName, Amount: amount },
dataType: 'json',
success: function (result) {
$('#transfare_error_list').html(result.text);
$('#trans_verification_id').val(result.id);
$('#transfare_username').val("");
$('#transfare_amount').val("");
},
error: function () {
$('#transfare_error_list').html('Oops... Error.');
}
});
}
});
I'm not 100% sure, but shouldn't you controller action handler be public ?

retrieve the result of an ajax request as a variable

How can I get the result of my ajax post into a variable:
function decode_original(hshdecode) {
var decode_original = 'decode=1&hashvalue=kjh4k5hq35l&hashkey=12345';
$.ajax({
type: "POST",
async: false,
contentType: "application/x-www-form-urlencoded; charset=utf-8",
url: 'decode_function.php',
data: decode_original,
cache: false,
success: function(return_value) {
var decoded_value = return_value;
console.log("Decoded Value:" +decoded_value);
return decoded_value;
},
error: function(data){
return data;
}
});
}
The above actually gets the return value successfully, but I am unable to pass the result into a variable:
var decode_value = decode_original(encoded_value);
alert(decode_value);
use responseText like :
success: function(return_value) {
var decoded_value = return_value.responseText;
console.log("Decoded Value:" +decoded_value);
return decoded_value;
}

$.ajax statusCode

I have a problem in my code . This is part of it:
function checkIfAvailable(username){
var url = "/checkAvail?user="+username;
var ans = $.ajax({
url: url,
type: "GET",
context: document.body,
statusCode: {
404: function() {
console.log("-1-1-1-1 WE GOT 404!");
},
200: function() {
console.log("-1-1-1-1 WE GOT 404!");
}
}
});
}
I think the response.status is 200, but I don't enter the '200' part.
So how can I print the response.status I'm getting?
success(data, textStatus, jqXHR){
var statusCode = jqXHR.status;
var statusText = jqXHR.statusText;
}
See jQuery API for more options...
function checkIfAvailable(username) {
var ans = $.ajax({
url: "/checkAvail",
type: "GET",
data: "user=" + username,
dataType: "html",//change it by the data type you get (XML...)
context: document.body,
statusCode: {
404: function() {
console.log("-1-1-1-1 WE GOT 404!");
},
200: function() {
console.log("-1-1-1-1 WE GOT 200!");
}
},
success: function() {
console.log("success");
},
error: function(e) {
alert(e);
}
});
}
Message inside console.log("-1-1-1-1 WE GOT 404!") is same for both 404 and 200. change the message for 200 like console.log("Success.....")

Resources