How to get current url query params in ajax request? - javascript

How can I add all current query parameters from current url to a jquery GET ajax request?
$.ajax({
type: 'GET',
url: "backend/count",
dataType: 'text',
success: function (rsp) {
...
}
}

$.ajax({
type: 'GET',
url: "backend/count" + window.location.search,

Related

How can i solve Ajax Ckeditor value post problem

I am trying to send a CKeditor value post with ajax but i cant response anyway! I cant find anything
function send_days(tourId){
var url = baseUrl + "tour/save_days/" + tourId;
var value = CKEDITOR.instances.tour_textarea_days.getData();
$.ajax({
url: url,
method: "POST",
data: dataString,
contentType: false,
processData: false,
cache:false,
success: function (data) {
$('.tour_popup_container').html(data);
}
});
}
but when i chance ajax method like this. It is succesfull
$.post(url, {data:value}, function (response) {
$('.tour_popup_container').html(response);
})
here is my codeigniter php file (it is not important actually)
public function save_days($tourId)
{
$value=$this->input->post("data");
print_r($value);
}
It looks like you used dataString instead of value.
var url = baseUrl + "tour/save_days/" + tourId;
var value = CKEDITOR.instances.tour_textarea_days.getData();
$.ajax({
url: url,
method: "POST",
data: value /*dataString*/,
contentType: false,
processData: false,
cache:false,
success: function (data) {
$('.tour_popup_container').html(data);
}
});

how to implement asynchronous get in ajax

what should I write in the script to make javascript asynchronously implement firstly ajax method GET and then another simple function(in the order of my script)?
because here while debugging I see that getCategories() is implemented before WriteCategories(categories)
$.ajax({
url: '/api/ListingAPI/GetCategoriesById?id=1',
type: 'GET',
dataType: 'json',
success: function WriteCategories(categories) {
// ...
}
});
var test = getCategories();
Actually you want synchronous execution,
you need to add async: false in ajax request, (But be aware of this it will freeze your UI until your request completes, actually it is not recommended)
$.ajax({
url: '/api/ListingAPI/GetCategoriesById?id=1',
type: 'GET',
async: false,
dataType: 'json',
success: function WriteCategories(categories) {
// ...
}
});
var test = getCategories();
or either you can call your function on ajax success callback,
$.ajax({
url: '/api/ListingAPI/GetCategoriesById?id=1',
type: 'GET',
dataType: 'json',
success: function WriteCategories(categories) {
var test = getCategories();
}
});
For your knowledge, You can pass your query string params in data option in ajax rather than query string itself,
var queryStringData = {id : 1}
$.ajax({
url: '/api/ListingAPI/GetCategoriesById',
type: 'GET',
dataType: 'json',
data: queryStringData,
success: function WriteCategories(categories) {
var test = getCategories();
}
});
Try this code.
$.ajax({
url: '/api/ListingAPI/GetCategoriesById?id=1',
type: 'GET',
dataType: 'json',
success: function WriteCategories(categories) {
var test = getCategories();
}
});
You can call this function after ajax success. By this way AJAX get will be executed first.

apply encodeuricomponent to all ajax calls in an application globally

I have ajax calls like this at several places in my application.
$.ajax({
type: 'POST',
url: url,
data: Json.stringify(Values),
dataType: 'json'
});
For these ones, I would like to add encodeURIComponent to data sent as below:
$.ajax({
type: 'POST',
url: url,
data: encodeURIComponent(Json.stringify(Values)),
dataType: 'json'
});
Is there any way that I can do this globally without manually editing it everywhere?
Create your own function for doing it.
var myAjax = function (options) {
if (typeof options.data !== "undefined") {
options.data = encodeURIComponent(options.data);
}
return $.ajax(options);
};
Then in your code replace:
$.ajax({ type: 'POST', url: url, data: Json.stringify(Values), dataType: 'json' });
With:
myAjax({ type: 'POST', url: url, data: Json.stringify(Values), dataType: 'json' });
Whatever you do, don't monkey patch!

trying to put an ajax request inside the success call back of another ajax request

I am trying to make two jQuery ajax calls, the second should be made from the success callback of the first. I have tried a couple variations of code e.g just messing with the brackets.
Here is what I tried.
$.ajax({
url: 'inc/grab_talk.php?name='+encoded_name+'&loc='+encoded_address+'&lat='+encoded_lat,
type: 'post',
success: function () {
$.ajax({
url: 'inc/talk_results.php',
type: 'post',
success: function (dat) {
alert(dat);
}
});
}
});
I am receiving '500 (internal server error) in console
Try this, you can use either POST or GET, but in your case GET seems to be more appropriate.
$.ajax({
type: "GET",
url: "some.php",
data: { name: "somename", location: "somelocation" },
success: function(){
$.ajax({
type: "GET",
url: "someother.php",
success: function(){
alert('test');
}
});
}
});
Check this example
$.ajax({
type: "post",
url: "ajax/example.php",
data: 'page='+btn_page,
success: function(data){
$.ajax({
var a=data; //edit data here
type: "post",
url: "example.php",
data: 'page='+a,
success: function(data){
});
});

Can't access element inside success ajax

I'm putting an ajax call inside an ajax call, but in the second, the element is not being recognized, example:
$.ajax({
url: 'controleFatAcoes.php',
type: 'post',
dataType: 'html',
data: {
acao: 'validaenviofat',
id_cliente: cli.id_cliente,
dt_fat: cli.id_fat
},
success: function(data) {
$.ajax({
url: 'controleFatAcoes.php',
data: {id_cliente: cli.id_cliente,
id_fat: cli.id_fat, acao: 'getdadosnf'},
type: 'post',
dataType: 'json',
success: function(dados) {
**$('#templateEmpresa').html(dados.empresa);**
}
)};
});
When I run a console.log($('#templateEmpresa')), I get:
[context: document, selector: "#templateEmpresa", constructor: function, init: function, selector: ""…]
Try this code out:
Might be a problem of scope given that you are executing the second ajax call inside the success function of the first ajax call.
var firstajaxsuccess = 0;
$.ajax({
url: 'controleFatAcoes.php',
type: 'post',
dataType: 'html',
data: {
acao: 'validaenviofat',
id_cliente: cli.id_cliente,
dt_fat: cli.id_fat
},
success: function(data) {
firstajaxsuccess = 1;
}
});
if(firstajaxsuccess){
$.ajax({
url: 'controleFatAcoes.php',
data: {id_cliente: cli.id_cliente,
id_fat: cli.id_fat, acao: 'getdadosnf'},
type: 'post',
dataType: 'json',
success: function(dados) {
**$('#templateEmpresa').html(dados.empresa);**
}
)};
}

Categories

Resources