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"];
?>
6
  • Would be helpful if you posted the PHP file. Commented May 28, 2015 at 10:26
  • are you running that another server on the same local computer on an another port? Commented May 28, 2015 at 10:32
  • php file should echo the response. and also check the data-type of response ('json' etc.) Commented May 28, 2015 at 10:32
  • Try to add this in your server config/.htaccess enable-cors.org/server_apache.html http://enable-cors.org/server_apache.html assuming you run apache Commented May 28, 2015 at 10:35
  • php runs on a server on port 8080 but the html file i just open in a browser Commented May 28, 2015 at 10:35

1 Answer 1

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/

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

4 Comments

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
@PaulBoon yes, as mentioned in answer, you can use jsonp. p.s. I have edited my answer.
i changed the code to $("button").click(function(){ alert("hoi"); $.ajax({ url: "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
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

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.