1

I'm trying to connect to a php file that is located at http://127.0.0.1:8080/projects/mssql/index.php with jquery's ajax method but I'm unable to get a response. What am I doing wrong?

I'm doing all of this local - do I have to change any settings in my php.ini file or is there an error in the code? Please help

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        alert("hoi");
        $.ajax({url: "http://127.0.0.1:8080/projects/mssql/index.php", async: false, success: function(result){
            $("div").html(result);
        }});
    });
});
</script>
</head>
<body>

<div><h2>Let AJAX change this text</h2></div>

<button>Change Content</button>

</body>
</html>

This is the php file. the php file itself works because I do get the expected response when I type it's location in my browser

<?php
    $serverName = "PAUL\SQLSERVER";
    $connectionInfo=$arrayName = array('Database' => "TestDatabase" );

    $conn = sqlsrv_connect($serverName,$connectionInfo);
    if($conn){
        echo "connection established";
    }
    else{
        echo "connection failure";
        die(print_r(sqlsrv_errors(),TRUE));
    }

    $sql="select custID from customer";
    $stmt = sqlsrv_query($conn,$sql);
    if($stmt == false){
        echo "Error retriving data";
        die(print_r(sqlsrv_errors(),TRUE));
    }
    echo "<br>";
    $row = sqlsrv_fetch_array($stmt);
    echo $row["custID"];
?>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Paul Boon
  • 143
  • 1
  • 12

1 Answers1

1

You can not make such call due to Cross Domain Origin policy. Please review below links to get more idea about that.

http://en.wikipedia.org/wiki/Same-origin_policy

You need to be on same domain to make AJAX calls. but in your case you are making call from browsing direct HTML file.

Adding some noted from that page:

In computing, the same-origin policy is an important concept in the web application security model. Under the policy, a web browser permits scripts contained in a first web page to access data in a second web page, but only if both web pages have the same origin. An origin is defined as a combination of URI scheme, hostname, and port number.[1] This policy prevents a malicious script on one page from obtaining access to sensitive data on another web page through that page's Document Object Model.

As a solution, you can use jsonp to make cross origin AJAX Calls: https://learn.jquery.com/ajax/working-with-jsonp/

Avinash
  • 6,064
  • 15
  • 62
  • 95
  • since i'm running everything local and i'm not worried about security is there some way i can enable the abilty to make ajax calls – Paul Boon May 28 '15 at 10:45
  • @PaulBoon yes, as mentioned in answer, you can use jsonp. p.s. I have edited my answer. – Avinash May 28 '15 at 10:46
  • i changed the code to $("button").click(function(){ alert("hoi"); $.ajax({ url: "http://127.0.0.1:8080/projects/mssql/index.php", async: false, jsonp: "callback", dataType: "jsonp", data: format: "json", success: function(result){ alert(result); }}); }); but i still dont get a result back and for some reason the alert doesnt go off anymore as well – Paul Boon May 28 '15 at 11:22
  • i have read up on this json p thing and on wikipedia it says i can only make get requests. is there soms other way to make cross domain ajax calls – Paul Boon May 28 '15 at 13:29