apply encodeuricomponent to all ajax calls in an application globally - javascript

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!

Related

i am trying to pass parameter to data through ajax

I am trying to pass value through an ajax json array but value of catergory variable is not getting in controller action
var category = $('#category').val();
var url = $('#ajax_action_search').val();
$.ajax({
type: "POST",
data: {
'category': category
},
dataType: "json",
cache: false,
contentType: false,
processData: false,
success: function(response) {}
});
You need to use the parameter namespace matching your extension/plugin:
$.ajax({
// ...
data: {
'tx_myext_foo[category]': category,
},
// ...
});
But you'll also need to configure the cHash evaluation since this will lead to a HTTP request like /?tx_myext_foo[category]=X which will fail without a matching cHash.
This can be done with the excludedParameters configuration option.
Please check the Controller action if category (parameter name) passed from the ajax is exactly same in the controller action too
var category = $('#category').val();
var url = $('#ajax_action_search').val();
$.ajax({
type: 'POST',
contentType: "application/json; charset=utf-8",
cache: false,
async: false,
url: url,
data: JSON.stringify {
category: category
},
dataType: 'json',
success: function(response) {}
});
You need to make ajaxurl with action and controller. and then pass the data in full format.
var ajaxUrl = '<f:uri.action action="youraction" controller="Yourcontroller" />';
var category = $('#category').val();
$.ajax({
type: 'POST',
url: ajaxUrl,
data: '&tx_yourext_yourplugin[category]='+category,
success: function(response) {
},
});
Just make the following changes :
var category = $('#category').val();
var url = $('#ajax_action_search').val();
$.ajax({
type: "POST",
url:url,
data: {'category': category},
dataType: "json",
success: function(response) {}
});

Two URLs with same AJAX structure

Is there any option to use two urls ((post & get) or (post & post)) in same AJAX structure ??? or some cooler format???
$.ajax({
type: "POST",
url: "save.php",
data: dataString,
success: function(chat_list) { }
});
$.ajax({
type: "POST",
url: "view.php",
data: dataString,
success: function(chat_list) { }
});
Sure, just use javascript variables.
var ajaxMethod = "POST";
var ajaxUrl = "save.php";
$.ajax({
type: ajaxMethod,
url: ajaxUrl,
data: dataString,
success: function(chat_list) {
}
});

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);**
}
)};
}

jsFiddle /echo/html not working with jQuery

http://jsfiddle.net/YcK5X/
I'm wondering why this AJAX request doesn't return anything.
$.ajax({
type: 'POST',
url: '/echo/html',
data: 'Echo!',
success: function(data) {
$('#ajax').html(data);
},
dataType: 'text/html'
});
The data you want echo:ed must be supplied in a POST parameter named html:
$.ajax({
type: 'POST',
url: '/echo/html/',
data: {
'html': 'Echo!'
},
success: function(data) {
$('#ajax').html(data);
},
dataType: 'text/html'
});

Categories

Resources