How to check ajax return value - javascript

I have an ajax call and it retunrs some value. Now i need to check the value in javascript. How can i do it
$('#cm').blur(function () {
var cmnumber = document.forms['myform']['cm'].value;
$.ajax({
type: 'get',
url: "/validatecm/" + cmnumber,
cache: false,
asyn: false,
data: cmnumber,
success: function (data) {
alert(data)
},
error: function (data) {
alert(data)
}
})
});
Now i need to access the "data" in my javascript submitformfunction. Any help wil be appreciated

success: function(data) {
console.log(data);
useInAnotherFunction(data);
}
function useInAnotherFunction(data)
{
...use here
}
If you use console.log(data); you can view clearly data what you have

You are already accessing the data here
alert(data)
If you still want to access it outside your success callback then make it like this
success: function(data) {
YourFunctionCall(data)
}

Related

this.setState not a function rails

componentDidMount() {
$.ajax({
type: "GET",
url: "/tweets",
dataType: "json",
success: function (data){
//$('#tweetsList').append(data);
this.setState({tweetsList: data});
},
error: function (error) {
console.log(error);
}
});
}
the above block of code gives me a this.setState function not found error. I am trying to render json from my Tweets controller to my main.jsx file.
The context of this changes in the ajax call. this no longer refers to your component. You need to reference the original context.
componentDidMount() {
const self = this;
$.ajax({
type: "GET",
url: "/tweets",
dataType: "json",
success: function (data){
//$('#tweetsList').append(data);
self.setState({tweetsList: data});
},
error: function (error) {
console.log(error);
}
});
}
The problem is this in your success callback function refers to that function and therefore doesn't have any setState method. You can pass context to jQuery ajax call like this:
$.ajax({
type: "GET",
url: "/tweets",
dataType: "json",
context: this,
success: function (data){
//$('#tweetsList').append(data);
this.setState({tweetsList: data});
},
error: function (error) {
console.log(error);
}
});
See jQuery ajax docs about context property for more info.

Javascript get JSON value from URL error

im using the function below to get image names. I also use the json code to get data of a different url, but somehow it isnt working at this. (im new to javascript. Just writing php normally.
function getImgname(name) {
$.getJSON("http://url.com/info.php?name="+name, function(json321) {
return json321.js_skininfo;
});
}
Try this:
function getImgname(myName) {
$.ajax({
url: 'http://url.com/info.php',
data: {
name: myName
},
type: 'POST',
dataType: 'json',
success: function(data) {
// do what you want with your data
return data.js_skininfo;
}
});
}
I tried this now:
function getImgname(myName) {
$.ajax({
url: "http://url.com/ninfo.php",
type: 'POST',
dataType: 'json',
success: function (data) {
return data.js_skininfo;
},
error: function () {
}
});
}
This isnt working (undefinied), but if i alert the data.js_skininfo it shows me the correct value.

Getting the value of a returned string from Ajax JQuery

I have a web service which adds an item to the database after being called using JQuery Ajax. The web service returns a string, and I can't manage to pick up only the string part returned. Instead I receive {"d":"The message I want to display"} using alert(data);.
I also tried alert(Object.keys(JSON.parse(data))[0]); which returns d and alert(Object.keys(JSON.parse(data))[1]); or alert(data.d); returns Undefined.Here's what my code looks like
function AddAjaxJQuery() {
var isbn = $('#<%= txtIsbn.ClientID %>').val();
var pdata = { "book": { "Isbn": isbn} };
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/BookWebService.asmx/InsertBook",
data: JSON.stringify(pdata),
dataType: 'text',
async: true,
success: function (data, textStatus) {
alert(data);
},
error: function (error) {
alert(data);
}
});
}
If your data is a string, then you should parse it to JSON first:
var dataInJson = JSON.parse(data);
alert(dataInJson.d)
You have to access property d of data (response) so Replace success callback with
success: function (data, textStatus) {
alert(data.d);
},

Scope variable in ajax call

Why the final console log is undefined?Variable time has a global scope and ajax call is async.
This is my code:
var time;
$.ajax({
async:"false",
type: 'GET',
url: "http://www.timeapi.org/utc/now.json",
success: function(data) {
console.log(data);
time=data;
},
error: function(data) {
console.log("ko");
}
});
console.log(time);
Change async to the boolean false.
http://api.jquery.com/jQuery.ajax/
var time;
$.ajax({
async: false,
type: 'GET',
url: "http://www.timeapi.org/utc/now.json",
success: function (data) {
console.log(data);
time = data;
},
error: function (data) {
console.log("ko");
}
});
console.log(time);
Also, note that if you need to use dataType: 'jsonp' here for cross-domain, you won't be able to synchronize -- so use a promise.
var time;
$.ajax({
dataType: 'jsonp',
type: 'GET',
url: "http://www.timeapi.org/utc/now.json",
success: function (data) {
time = data;
},
error: function (data) {
console.log("ko");
}
})
.then(function(){ // use a promise to make sure we synchronize off the jsonp
console.log(time);
});
See an example like this here using Q.js:
DEMO
In your code, you initialize the global variable 'time' as 'data'. Where does this data variable come from? If the data variable is not global also, when you try to use console.log(time); , it may be undefined because the data variable is undefined.
Make sure both variables are within scope to be used globally. That might work. Good luck!
OK, a bit contrived, but I hope it shows the point regarding scope of time and timing ...
$.ajax({
async: false,
dataType: 'jsonp',
type: 'GET',
url: "http://www.timeapi.org/utc/now.json",
success: function (data) {
console.log(data.dateString);
time = data.dateString;
},
error: function (data) {
console.log("ko");
}
});
window.setTimeout("console.log('time: '+time)",3000);
JSfiddle

Variable not working in simple Ajax post

Can't seem to get the variable getID to work. I'm trying to change the html of the div. I know that the variable has the right value.
$('.cardid').change(function() {
var getID = $(this).attr('value');
$.ajax({
type: "POST",
url: "inc/change_thumbnail.php",
data: "id="+getID,
cache: false,
success: function(data) {
$("#"+getID).html(data);
alert("success");
},
error: function (err) {
alert("error");
}
});
});
Write data in $.ajax as data: {id : getID}, instead of data: "id="+getID,
Use val to get the value of an input :
var getID = $(this).val();
As you're making a POST request, you should also use the data argument to let jQuery properly send the value :
$.ajax({
type: "POST",
url: "inc/change_thumbnail.php",
data: {id:getID},
cache: false,
success: function(data) {
$("#"+getID).html(data);
alert("success");
},
error: function (err) {
alert("error");
}
});
You can try this:
$('[id="'+getID+'"]').html(data);
and yes you should pass it this way:
data:{id:getID}

Categories

Resources