kingcope kingcope - 6 months ago 65
Ajax Question

jquery/ajax autologin to website

I have this code in python :

import requests

session=requests.Session()

def login_mps_api(username, password):
MyHeaderss = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.32 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.32",
"X-GWT-Permutation" : "6FEFBE57C6E73F0AB33BD5A4E17945DE",
"Content-Type":"text/x-gwt-rpc; charset=utf-8",
"X-GWT-Module-Base": "https://www.cartetitolari.mps.it/portaleTitolari/",
"Referer": "https://www.cartetitolari.mps.it/portaleTitolari/titolari.html"}


login_data = '''7|0|7|https://www.cartetitolari.mps.it/portaleTitolari/|FEAC78FFDF81D6121438D70986AF1C41|portale.titolari.client.service.PTService|login|portale.titolari.client.common.login.LoginRequest/3583069702|{password}|{username}|1|2|3|4|1|5|5|0|0|6|7|'''.format(username=username, password=password)
ra0=session.post('https://www.cartetitolari.mps.it/portaleTitolari/service', data=login_data, headers=MyHeaderss)


I need to make a similar request using ajax/jquery, the problem is that I need to use specific headers like in my python code, how can i do this using jquery ?

Answer

UPDATED

here's how you go about it:

var MyHeaderss = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.32 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.32",
              "X-GWT-Permutation" : "6FEFBE57C6E73F0AB33BD5A4E17945DE",
              "Content-Type":"text/x-gwt-rpc; charset=utf-8",
              "X-GWT-Module-Base": "https://www.cartetitolari.mps.it/portaleTitolari/",
              "Referer": "https://www.cartetitolari.mps.it/portaleTitolari/titolari.html"};
$.ajax({
    type:"POST",
    beforeSend: function (request)
    {
      request.setRequestHeader(MyHeaderss);
    },
    url: 'https://www.cartetitolari.mps.it/portaleTitolari/service',
    data: login_data,
    success: function(databak) {
    }
});