1

First I login successfully and a cookie set by server in my browser using following snippet :

var form = new FormData();
form.append("email", "pmsoftwares@hotmail.com");
form.append("pword", "pass123");
form.append("geo", "6.4636835,3.8494566");

var settings = {
  async: true,
  crossDomain: true,
  url: "https://krdly-web-dev0.cfapps.io/session",
  method: "POST",
  processData: false,
  contentType: false,
  mimeType: "multipart/form-data",
  data: form
};

$.ajax(settings).done(function(response) {
  console.log(response);
});

but i got "500 : Internal server error" when i use below code for getting user profile:

var form = new FormData();
form.append("email", "pmsoftwares@hotmail.com");
form.append("pword", "pass123");
form.append("geo", "6.4636835,3.8494566");

var settings = {
  async: true,
  crossDomain: true,
  url: "https://krdly-web-dev0.cfapps.io/user",
  method: "GET",

  xhrFields: {
    withCredentials: true
  },

  processData: false,
  contentType: false,
  mimeType: "multipart/form-data",
  //data: form
};

$.ajax(settings).done(function(response) {
  console.log(response);
});

the following code is given by postman but it does'nt works in browser :

var form = new FormData();
form.append("email", "pmsoftwares@hotmail.com");
form.append("pword", "pass123");
form.append("geo", "6.4636835,3.8494566");

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "https://krdly-web-dev0.cfapps.io/user",
  "method": "GET",
  "headers": {
    "cache-control": "no-cache",
    "postman-token": "70f8a8cf-6a8f-4905-aaed-69acaeb63cd2"
  },
  "processData": false,
  "contentType": false,
  "mimeType": "multipart/form-data",
  "data": form
}

$.ajax(settings).done(function(response) {
  console.log(response);
});
georgeawg
  • 46,994
  • 13
  • 63
  • 85
Mubsher Mughal
  • 402
  • 6
  • 18
  • 1
    I would remove your tokens, link and other sensible info from your question. I tried to open this link - https://krdly-web-dev0.cfapps.io/user - it gives 500. Maybe everything is ok with your code and the problem is on the server side? – curveball Aug 10 '17 at 18:01
  • please someone make this work ,i'll be very thankful if a jquery expert would do this : also can discuss on skype : pmsoftwares@hotmail.com – Mubsher Mughal Aug 10 '17 at 18:07
  • it works fine on postman and return the user details,not api issue – Mubsher Mughal Aug 10 '17 at 18:09
  • the 500 its from the GET or appear OPTIONS – Jesus Carrasco Aug 10 '17 at 18:17
  • @Jesus Carrasco if i set headers as from postman code above ,it says OPTIONs and return 405,and if i remove headers it says GET and return 500 – Mubsher Mughal Aug 10 '17 at 18:40
  • check out these questions and answers in case you haven't done it yet: https://stackoverflow.com/questions/13287949/options-405-method-not-allowed-regardless-server-sends-access-control-allow-me https://stackoverflow.com/questions/13624386/handling-cors-preflight-requests-to-asp-net-mvc-actions/13646169#13646169 – curveball Aug 10 '17 at 19:32
  • these were server side QAs i am looking for client side solv... – Mubsher Mughal Aug 10 '17 at 20:32
  • @mubsher: in general we discourage question-askers from seeking a private support channel here. The point of Stack Overflow is to help in public, so other people can learn from the answers given. Readers also do not take code and magically make it work - they will give you hints and assistance, and it is your job to fix it. – halfer Aug 10 '17 at 20:36
  • 1
    @halfer,okay i understand the terms... – Mubsher Mughal Aug 10 '17 at 20:49

1 Answers1

0

For some reason the cookies sent by the POST ajax request are not set by the browser. Simply add the below to the ajax POST request :

xhrFields: {
    withCredentials: true
},

to fix the issue. Following is my code :

<!doctype html>
<html>
<head>
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js">
  </script>
</head>
<body>
  <div class="">
      <button type="button" name="button">Send Ajax Request</button>
  </div>
  <script type="text/javascript">
    $(document).ready(function(){
      $("button").click(function(){
        // sendSecondRequest()
          // .always(sendFirstRequest)
          // .always(sendSecondRequest);
        sendFirstRequest().done(sendSecondRequest);
      });
    });
    function sendFirstRequest(){
      var form = new FormData();
      form.append("email", "pmsoftwares@hotmail.com");
      form.append("pword", "pass123");
      form.append("geo", "6.4636835,3.8494566");

      var settings = {
        async: true,
        crossDomain: true,
        url: "https://krdly-web-dev0.cfapps.io/session",
        method: "POST",
        processData: false,
        contentType: false,
        mimeType: "multipart/form-data",
        data: form,
        xhrFields: {
          withCredentials: true
        }
      };

      return $.ajax(settings).done(function(response) {
        console.log(response);
      });
    }

    function sendSecondRequest(){
      var form = new FormData();
      form.append("email", "pmsoftwares@hotmail.com");
      form.append("pword", "pass123");
      form.append("geo", "6.4636835,3.8494566");

      var settings = {
        async: true,
        crossDomain: true,
        url: "https://krdly-web-dev0.cfapps.io/user",
        method: "GET",

        xhrFields: {
          withCredentials: true
        },

        processData: false,
        contentType: false,
        mimeType: "multipart/form-data",
        //data: form
      };

      return $.ajax(settings).done(function(response) {
        console.log(response);
      });
    }
  </script>
</body>

References :

https://stackoverflow.com/a/7189502/566092

http://aleembawany.com/2006/11/14/anatomy-of-a-well-designed-ajax-login-experience/

coding_idiot
  • 12,860
  • 9
  • 56
  • 111
  • strange, I tried again in chrome incognito and it worked fine. Are you able to see the cookies using some chrome cookie extension for the target domain after the first request ? – coding_idiot Aug 11 '17 at 18:41
  • @mubsher You can always accept the answer & upvote if it worked for you :-). – coding_idiot Aug 13 '17 at 16:38
  • the method have a drawback,my system has many request and each request will require login in this case,instead i want to call the session once and use the same cookie generated by session request in each subsequent request ... – Mubsher Mughal Aug 15 '17 at 07:34