I want get ajax data parameter at success callback function
like bellow
$.ajax({
url: 'example.com/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
}
});
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.com/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
// want get data parameter // {'sample':'test'}
Pass data
as a property to jqxhr
object at beforeSend
, get object at success
.
$.ajax({
url: 'example.com/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
}
})
Other solution, you can put data into variable directly :
var dataObj = {
sample:'test'
}; // => I want this data in success function.
$.ajax({
url: 'example.com/something',
method: 'GET',
data: dataObj,
success: function(response, textStatus, jqXHR){
// then just console.log(dataObj.sample)
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.