0

my css defines an input of type button submit, and I have a js function for when it gets clicked

$(".submit").click(function(e){
    if (!e.isDefaultPrevented()) {
            var url = "saveProfile.php";

            $.ajax({
                type: "POST",
                url: url,
                data: $(this).serialize(),
                success: function (data)
                {
                    //alert('success function');
                    var messageAlert = 'my_alert-' + data.type;
                    var messageText = data.message;

                    var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + messageText + '</div>';
                    var alertBox2 = '<div align="center" class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + messageText + '</div>';
                    //alert(messageAlert);
                    alert('Thanks :)');

                    if (messageAlert && messageText) {
                        $('#msform').find('.result_message').html(alertBox2);
                        $('#msform')[0].reset();
                    }
                }
            });
            return false;
        }

    return false;
})

the problem - ajax function always fails the error message: doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource

What I've noticed: I can't see the saveProfile.php file on the side panel - does that mean it isn't loaded and if so why? it is located in the same directory as the js file calling/referring to it.

Kukula Mula
  • 1,788
  • 4
  • 19
  • 38
  • 2
    Have you tried debugging it using chrome/firefox dev tools ? Just go to the `Network` tab and see what is happening with your ajax – gogaz Sep 21 '17 at 07:53
  • Also add error function. Never assume that your code will always work. Make a habit of using try catch statement. – Rotimi Sep 21 '17 at 08:02
  • Are you testing it locally, on a machine in your LAN, or on an external domain? What's the full URL you're using? – DarthJDG Sep 21 '17 at 08:20

3 Answers3

0

I consider that the *.php files should locate the same directory the js file be called/referred.

And you can also add a header in your request header('Access-Control-Allow-Origin: *'). but this may be unsafe.you can do more better from how to bypass Access-Control-Allow-Origin?

shanechiu
  • 85
  • 3
  • 12
0

saveProfile.php file might not be located in your document root. In that case, you should give the relative url of saveProfile.php from your root as the value of url variable.

-3

Use the network tab of the browser debugger (for example chrome devTools). There all requests are listed. You can see there if the request was successful or not.

HolgerJeromin
  • 2,201
  • 20
  • 21
  • Why is my answer voted down? IMHO it is better to show a user how to debug an application that guessing wild what could be a problem and give solutions to the hypothetical problems. – HolgerJeromin Sep 26 '17 at 12:13