Adding a variable to all ajax calls - ajax

I have a variable that I wish to add to all my ajax calls, how is this possible.
I have tried the ajaxSetup like this:
$.ajaxSetup({
data: {'language': 'ENU'}
});
But the preset variable (language) gets overwritten as soon as I specify the data parameter in the actual call.

According to jQuery API documentation "All subsequent Ajax calls using any function will use the new settings, unless overridden by the individual calls, until the next invocation of $.ajaxSetup().".
This should be true for the data attribute to. Make sure you don't specify data for your individual ajax calls or read the data before setting it to verify that your set default is there and then append to that.

Related

Multiple ajax call with different url in one function

In the 1st ajax call I am storing output of a service in a variable. That is working fine. Then I have to use that variable as input in another service, different url. Is it possible to write in same ajax function?
And if possible, then how to do that?
This can be done using promises. In this case you'd have to return a promise from the function.

Intercepting jQuery ajax

If I am going to encrypt the data being passed by jQuery ajax prior to sending to the network (regardless if the network is SSL'd or not), where can I inject that functionality?
You can intercept all ajax queries initiated by jQuery, examine their content and change them (encrypt in your case) by using ajax prefilter in jQuery. With is approach you can modify all requests' contents at one place globally.
Details: http://api.jquery.com/jQuery.ajaxPrefilter/
Well, jQuery.ajax has a data field, which corresponds to the data sent in your request. So cleanest would be to set this field as a call to your encoding function.
$.ajax({
...
data: yourDataEncodingFunction(),
...
});
Remember data must be Key/Value pairs, so be sure that's what your function returns.

How to call an action inside another action in Yii?

I've a situation where i get some information from database and based on the data , i want to do/forward to some other controller & action.
How can i do this using Yii? Its like an ajax request..
If i can use the CController->forward() , then how can use the post values for actions?
I shall assume that the reason why redirect() didn't work for you was because you can't sent post variables with it. If that's the case, then let me show you how to overcome the lack of POST support in redirect(). You can use setState(). It creates variables that simulate POST variables. This is the code to store or set a variable:
Yii::app()->user->setState('var', 'value');
And, in order to trace the value you just code as follows:
Yii::app()->user->getState('param1');
It would equally work with forward, but I'm not sure why you want to use it instead of redirect().

Is it possible to send a form and a html request with same Event by Mootools?

$('submitbutton').addEvent( 'submit', function(e){
e.stop();
$('fuss').send();
req2.send();
});
trying to get this working but not sure if it is possible and had no success so far.
Mootools docs doesnt helped me either.
Will the multiple usage of .send() work?
Do i have to specify the data beeing send for the html request or does it take automatical the data beeing send by the form ?
It's in the documentation: http://mootools.net/docs/core/Request/Request#Element:send
This shorthand method will do a request using the post-processed data from all of the fields.
You can do as many requests in a specific event as you wish, as long as they're all asynchronous.
In your specific example it would seem you want to do two requests using two different methods, one with setting up a new Request class manually and the second doing it via the Element Method.
Based on your last comment, I wrote a little example in jsFiddle.
Basically I think you don't need two request for your goal. Just override onRequest method to update the html.

jquery ajax data string. How do i make an object or escape the string?

I have a button which calls jquery.ajax and submits POST data. I need to get user text from a textarea. So far, no problem. However now i need to set the post data. I have a string in the form as k=v&k2=v2 etc and then i have this user text. I obviously cant write + "&usertext=" + usertext since the text may have code and &kxxx=val which should be inside of the usertext value.
How do i set the ajax data?
http://api.jquery.com/serialize/
You can call the jQuery method serialize on a form to get a valid data structure to pass in to jQuery.ajax instead of building it from scratch:
$.ajax({
data: $('#myform').serialize()
});
For the values outside of your textarea you can then add them as hidden inputs in your form and they'll be pulled in to the resulting serialize data.
there are at least 2 options:
1. use http://jquery.malsup.com/form/ jQuery plugin, see 'ajaxSubmit' method
2. use 'serialize' method of jQuery (seee here for details)

Resources