I want get ajax data parameter at success callback function
like bellow
$.ajax({
url: 'example/something',
method: 'GET',
data: { 'sample':'test' }, // => I want this data in success function.
success: function(response, textStatus, jqXHR){
// want get data parameter
// {'sample':'test'}
// someone answered that could get below code but I couldn't get it
// console.log(this.data); => X
}
});
I want get ajax data parameter at success callback function
like bellow
$.ajax({
url: 'example./something',
method: 'GET',
data: { 'sample':'test' }, // => I want this data in success function.
success: function(response, textStatus, jqXHR){
// want get data parameter
// {'sample':'test'}
// someone answered that could get below code but I couldn't get it
// console.log(this.data); => X
}
});
Share
edited Feb 14, 2017 at 6:03
Shaffron
asked Feb 14, 2017 at 5:49
ShaffronShaffron
1071 gold badge3 silver badges11 bronze badges
5
- What you want exactly? Its not clear what you want still. – Sorangwala Abbasali Commented Feb 14, 2017 at 5:51
-
remove
this
and doconsole.log(data)
– Sergio Alen Commented Feb 14, 2017 at 5:52 - oh! I'm really sorry about confused question. I fix my question. – Shaffron Commented Feb 14, 2017 at 6:03
- The AJAX generally returns JSON object you need to parse that data something like this "var obj = JSON.parse(text);" – srikanth r Commented Feb 14, 2017 at 6:06
-
You are trying to get value passed at
data
setting atsuccess
, correct? – guest271314 Commented Feb 14, 2017 at 6:19
6 Answers
Reset to default 1// want get data parameter // {'sample':'test'}
Pass data
as a property to jqxhr
object at beforeSend
, get object at success
.
$.ajax({
url: 'example./something',
method: 'GET',
data: {
"sample": "test"
},
beforeSend: function(jqxhr, settings) {
jqxhr._data = settings.url.split("?").pop();
},
success: function(data, textStatus, jqXHR) {
// want get data parameter
// {'sample':'test'}
var _data = jqXHR._data.split("&").slice(0, 1).pop().split("=");
var obj = {}; obj[_data[0]] = _data[1];
console.log(_data, obj);
// var obj = {};
// someone answered that could get below code but I couldn't get it
// console.log(this.data); => X
}
})
Should just be able to type data.sample
in the success handler to get the value. Alternately, you could do data['sample']
, but that syntax really exists more for allowing you to store property names in variables for retrieving data from objects.
Change method
to type
and console.log(data)
to view the content of data
passed to the callback function.
Also, add dataType: 'json'
in the call.
$.ajax({
url: 'example./something',
type: 'GET',
dataType: 'json',
data: { 'sample':'test' },
success: function(data, textStatus, jqXHR){
console.log(data); // will display the content of data
},
error: function(a,b,c) {
console.log('something went wrong:',a,b,c);
}
});
Instead of this.data
, in your case, to access the value "test", you'd need to use data.sample
.
Try looking into JSON objects, as that is generally what AJAX requests will be returning. You're going to using "dot notation" a lot in JS.
What's your backend code? java c# php? if c# and mvc controller,you can defination an action the pramater in the action will be you ajax pass
Other solution, you can put data into variable directly :
var dataObj = {
sample:'test'
}; // => I want this data in success function.
$.ajax({
url: 'example./something',
method: 'GET',
data: dataObj,
success: function(response, textStatus, jqXHR){
// then just console.log(dataObj.sample)
}
});