0

Why ajax post can not load page when use short url ?

When i use short URL test.php in ajax post like this

<script>
function send_username_value_for_check_available() {
        $.ajax
        (
            {
                url: 'test.php',
                type: 'POST',
                data: $('#username_send_value_fid').serialize(),
                cache: false,
                success: function (data) {
                    $('#mySpan_username').show();
                    $('#mySpan_username').html(data);
                }
            }
        )
}
</script>

i will get error

XMLHttpRequest cannot load https://example.com/test.php. Response for preflight is invalid (redirect)

But when i use full URL https://www.example.com/test.php it's work good.

How can i do for use short URL in ajax post ?

2 Answers2

0

Your short URL test.php resolves to example.com/test.php
example.com and www.example.com are seen as different domains. So the ajax request is treated like a cross-domain call.
Try authorizing example.com and/or www.example.com in your script with the Access-Control-Allow-Origin header (you should avoid allowing '*'). Using php:

header("Access-Control-Allow-Origin: https://example.com");

For further information:
- "CORS response headers" might be search-keywords to help you on this subject.
- Also have a look a this answer: https://stackoverflow.com/a/8689332/3872061

Community
  • 1
  • 1
fpierrat
  • 739
  • 7
  • 25
0

I think this has to do nothing with AJAX with short URK issue...

You have to allow CORS from your server for this Domain and it will work

http://enable-cors.org/server_php.html

Gaurav joshi
  • 1,743
  • 1
  • 14
  • 28