1

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);

?>
2
  • I tried changing the dataType to jsoup as well as json but that did not work ? Commented Mar 9, 2014 at 4:27
  • 1
    Unless you enable cross-origin (CORS) support on your server, you will have to switch to JSONP and change your server to support the callback that JSONP uses and package the results into that callback. Commented Mar 9, 2014 at 4:29

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Castt thanks for the response adding Access-Control-Allow-Origin: * to my php file worked but like you said instead of * i need to put trusted domains. How will that work for my app because i am going to convert my html file to .apk for andriod app and then distribute it on the market ?
It shouldn't be an issue, as long as you specify only your trusted server in the header. This way your app will be much secure and will be protected from XSS injections. As mentioned above, you might consider JSONP as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.