Intercepting jQuery ajax - 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.

Related

Adding a variable to all ajax calls

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.

Client side to server side calls

I want to change the list of available values in a dropdown depending on the value selected in another dropdown and depending on values of certain fields in the model. I want to use JQuery to do this. The only hard part is checking the values in the model. I have been informed that I can do this using Ajax. Does anyone have any idea how I will approach doing this?
AJAX is indeed the technology your looking for. It is used to sent an asynchronous request from the client browser to the server.
jQuery has an ajax function that you can use to start such a request. In your controller you can have a regular method tagged with the [HttpPostAttribute] to respond to your AJAX request.
Most of the time you will return a JSON result from your Controller to your view. Think of JSON as something similar to XML but easier to work with from a browser. The browser will receive the JSON and can then parse the results to do something like showing a message or replacing some HTML in the browser.
Here you can find a nice example of how to use it all together.

JSONP, do you have to change your JSON file?

Can someone help me understand JSONP a little better?
I have a json file being out putted to a url. But due to same-origin policy I need to use JSONP. My question is do I need to change the actual JSON file or will using an ajax call with jquery, dataType: 'jsonp' do the work for me?
JSONP is nothing but, JSON with padding i.e. JSON wrapped by a function call. This format helps to pass the JSON data to java script.
JSON came into picture, when the JSON i.e. java script object can be used to represent the data, which was previously represented in the form of XML.
For example,
var data={...}; is data in json format. Whereas In JSONP, same data is written as getData(data);
In your scenario of ajax call, dataType:'jsonp', json data has to be passed as an argument to a function. You can access the response in that function.
If you could have provided some code, it will be easy to resolve your query. Information about JSONP is available on wikipedia here.
You will have to wrap your JSON data in a function call.
Like, someFunctionName(YOUR_EXISTING_JSON_DATA);
And,
use someFunctionName as jsonp callback
See, Cross-domain communications with JSONP

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