1

I have a site where when the page load it should he hit a php file. I am using following code of jquery ajax to pass data on php file that give me some response. but it return nothing in Mozilla. But its workes perfect in IE and Chrome Brwser. Any one can help me?

   <script>
        window.onload = MyFunction;

        var myString = "NO";

        function MyFunction2() {   // this function will be call on button click
            // alert("button clicked");
            myString = "Yes";

            $("#myModal").removeClass("in");
            $("#myModal").removeClass("fade");
            $(".modal-backdrop").remove();
            $("#myModal").hide();

            $('#myfadeDiv').removeClass('in');
            $('#myfadeDiv').removeClass('fade');
            $('#myfadeDiv').removeClass('modal-backdrop');

            MyFunction();
            location.href = 'http://www.MyExampleWebsite.com/';
        }



        // function submitQuery() {
        function MyFunction() {
            $.ajax({
                type: "POST",
                data: ({ result: myString }),
                url: "http://www.MyExampleWebsite.com/try.php",
                success: function (html) {
                    if (html == "  ") {
                        debugger;

                        $("#myModal").css("display", "block");
                        $('#myModal').addClass('in');
                        $('#myModal').addClass('fade');
                        $("#myModal").show();

                        $('#myfadeDiv').addClass('in');
                        $('#myfadeDiv').addClass('fade');
                        $('#myfadeDiv').addClass('modal-backdrop');
                    }
                    else {
                        debugger;
                        $("#myModal").removeClass("in");
                        $("#myModal").removeClass("fade");
                        $(".modal-backdrop").remove();
                        $("#myModal").hide();

                        $('#myfadeDiv').removeClass('in');
                        $('#myfadeDiv').removeClass('fade');
                        $('#myfadeDiv').removeClass('modal-backdrop');
                    }
                }
            });
            return false;
        }

        function closewindow() {
            if (confirm("Thank You. You May Close The Window.")) {
               // close();
                window.top.close();
                window.parent.close();
                var win = window.open("", "_self"); /* url = “” or “about:blank”; target=”_self” */
                win.close();
                window.close();
            }
        }
    </script>

try.php

<?php header('Access-Control-Allow-Origin: *');

   session_start();

   $msg2 = $_POST['result'];

   if($msg2=='Yes')
   {
   //session_start();


   if( isset( $_SESSION['counter'] ) ) {
      $_SESSION['counter'] += 1;
   }else {
      $_SESSION['counter'] = 0;
   }
    }
   $msg = $_SESSION['counter'];



?>
 <?php  echo ( $msg ); ?>
Durgesh Pandey
  • 2,314
  • 4
  • 29
  • 43
  • setting the origin header in your try.php is pointless. it has to be set in th page that sends that JS code to the browser. after all, without the header saying the browser is allowed to that cross-domain request, it's not going to do a cross-domain request to see if it's allowed to do the cross-domain request. "Hey dad, I'm at the mall. am I allowed to go to the mall?" – Marc B Feb 11 '16 at 18:44
  • ok, but how to solve this problem... – Durgesh Pandey Feb 11 '16 at 18:49
  • @MarcB — Eh? That isn't how CORS works. The page you are making the request to has to grant permission. If it worked the way you suggested that I would be able to have my website tell the user's browser that my JavaScript is allowed to read data from their online banking … which would be terrible. – Quentin Feb 12 '16 at 16:17
  • "it return nothing in Mozilla" — You need to do some basic debugging. Use the Developer Tools in your browser. What does the Console say? Errors get reported there. Look at the Network tab. Does your Ajax request should up? Is it a POST request? Does it contain the data you expect? Does it get the response you expect? – Quentin Feb 12 '16 at 16:18

0 Answers0