0

I want all the ajax params before sending an ajax request in JSON format and I need to encrypt each value in JSON and again pass to the ajax request.

I get data in URI format as see in below code, not in JSON. How can I get that?

Around 200 Ajax in this format:

$.ajax({
    type: "POST",
    url: site_url + "user/user/login_action",
    data: login_parms,
    success: function (data) {
    },
    error: function (xhr, textStatus, errorThrown) {
    }
});

Before Ajax Call:

$(document).ajaxSend(function(event, jqxhr, settings) {
    console.log("settings :",settings.data);
});

Console log:

settings : vEmail=disha.c1%40grr.la&vPassword=123456789

Also if in AJAX use formData then how we can get each value of form data?

Ishan Shah
  • 1,665
  • 2
  • 20
  • 42
  • Can you show us the rest of the code? – Jan Lois Feb 12 '19 at 13:14
  • Maybe read up on the documentation provided by the people behind jQuery. – Mouser Feb 12 '19 at 13:14
  • Depends on how the $.ajax was setup - you probably want to wrap $.ajax with your own implementation rather than try and intercept all requests. – freedomn-m Feb 12 '19 at 13:32
  • @JanLois, in Rest of the code, nothing is there because i didnt find how to get JSON for same. – Ishan Shah Feb 13 '19 at 04:59
  • @Mouser, I read but didnt find so posted over here. – Ishan Shah Feb 13 '19 at 04:59
  • @freedomn-m, mean can you please explain me in depth, AS i have used standard syntex of the AJAX – Ishan Shah Feb 13 '19 at 04:59
  • 1
    why not encrypted your parameters first and then pass in ajax call using request type post and datatype json. – DHARMENDRA SINGH Feb 13 '19 at 05:16
  • @DHARMENDRASINGH, First we have done all the project in that way before 2 years and now we have to encrypt that all fields, So that is not a good practics to find all the ajax call and do manually, instead of that i am finding global solution for same – Ishan Shah Feb 13 '19 at 05:24
  • 1
    "standard syntax of ajax" - a GET will put the parameters in the URL (as there's no body) - a POST will put them in the body. You could even be using a PUT or `$.load` / `$.post` / `$.ajax`. Parameters will be encoded differently if you use `traditional:true`. Parameters will be encoded differently if you use `stringify` vs an object. Your code could be manually adding parameters when setting the url, so they won't be "parameters" to the ajax call. All of these are "standard syntax" and all need to be handled differently. – freedomn-m Feb 13 '19 at 05:31
  • @freedomn-m, I have updated my question. please look into that – Ishan Shah Feb 13 '19 at 05:49
  • Possible duplicate of [intercept all ajax calls?](https://stackoverflow.com/questions/6884616/intercept-all-ajax-calls) – Ekown Feb 13 '19 at 08:05
  • @Ekown, My question is different as i need to get data in JSON or object not in the string. – Ishan Shah Feb 13 '19 at 08:58

1 Answers1

0

If you want to send a AJAX JSON CALL you must to use:

$.ajax({
    type: "POST",
    url: site_url + "user/user/login_action",
    dataType: "json",
    async: false,
    contentType: "application/json",
    data: JSON.stringify(login_parms),
    success: function (data) {
    },
    error: function (xhr, textStatus, errorThrown) {
    }
});

if you want to modify the param:

$.ajax({
  beforeSend: function(xhr){
    this.data
  }
});
Ciccio
  • 468
  • 4
  • 17
  • Yes, but still the same again, i have to change each and every ajax request to add datatype,contentType etc. So is there any middleware? – Ishan Shah Feb 13 '19 at 09:29
  • Sure with ajaxsetup $.ajaxSetup({ dataType: "json", contentType: "application/json", }); – Ciccio Feb 13 '19 at 14:43