0

What is preventing type POST from getting the expected result if using location.href (or window.location.href) as data?

This code is not working (it returns nothing):

$.ajax({
    type: "POST",
    url: 'page.asp',
    data: {'q': location.href},
    success: function(short_link,status){ 
        console.log('short_link = '+short_link);
        console.log('status = '+status);
    }
});

enter image description here

I do see q value is sent in browser Network info, BUT somehow it looks like the POST is redirected to a GET because of an 302 Object moved status code. Much like stated in there: Returning redirect as response to XHR request

enter image description here

However, if I change it to data: {'q': 'to some string'} it works.

Also, if I change the code to use GET (and the code in 'page.asp' accordingly) it also works:

$.ajax({
    type: "GET",
    url: 'page.asp?q='+location.href,
    success: function(short_link,status){ 
        console.log('short_link = '+short_link);
        console.log('status = '+status);
    }
});
MeSo2
  • 450
  • 1
  • 7
  • 18

2 Answers2

0

I think you have to encode url, to replace special chars.

Dexxo
  • 121
  • 1
  • 4
  • I added this `contentType : 'application/x-www-form-urlencoded',` and set `var q = encodeURIComponent(location.host)`. Still not working. – MeSo2 Feb 07 '20 at 21:27
0

I was looking in the wrong place.

The page.asp had a check for SQL injection function that was causing the error.

MeSo2
  • 450
  • 1
  • 7
  • 18