unable to make cross domain calls using jquery ajax method?

106 views Asked by At

The below JQuery Ajax method does not work when i try to make a call from my localhost using wamp to a php file which is located on a remote webserver. However it works fine if they both are on the same webserver. I believe i have turned on the crossDomain still not able to make cross domain calls ?

  <script>
             $(function() {

                $("#callAjax").click(function() {
                    var theName = $.trim($("#theName").val());

                    if(theName.length > 0)
                    {
                        $.ajax({
                          type: "GET",
                          url: "http://www.bcbustransit.uni.me/callajax.php",
                          data: ({name: theName}),
                          crossDomain: true,
                          cache: false,
                          dataType: "text",
                          success: onSuccess
                        });
                    }
                });

                $("#resultLog").ajaxError(function(event, request, settings, exception) {
                 $("#resultLog").html("Error Calling: " + settings.url + "<br />HTTP Code: " + request.status);
                });

                function onSuccess(data)
                {
                    $("#resultLog").html("Result: " + data);
                }

            });


        </script>

.

<?php

$con=mysqli_connect("freehosting","xyz","xyz","xyz","3306");

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }


mysqli_select_db($con,"u197344625_cfv");

$result = mysqli_query($con,"SELECT * FROM cfv_businfofull WHERE busnumber = 1 ");

echo "<table border='1'>
<tr>
<th>Bus Number</th>
<th>StopNames</th>
<th>Time</th>
<th>Day Of Week </th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['BusNumber'] . "</td>";
  echo "<td>" . $row['StopNames'] . "</td>";
  echo "<td>" . $row['Timings'] . "</td>";
  echo "<td>" . $row['DayOfWeek'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);

?>
1

There are 1 answers

2
Sergey Tsibel On BEST ANSWER

You should take a look at CORS.

In your case, you can just add Access-Control-Allow-Origin: * header with response on your Server side. Note, that instead of * you should use only trusted domains.