0

I was wondering if anyone can give me a hand with cross domain calls with JSON, I know you have to use JSONP or ajax, but not quite to sure how to. I have a PHP file that grabs entries from MySQL database and builds them into a php file like so

api.php

    <?php
    $link = mysql_pconnect("localhost", "root", "") or die("Could not connect");
    mysql_select_db("blakewilson") or die("Could not select database");

    $arr = array();

    $rs = mysql_query("SELECT * FROM products");

    while($obj = mysql_fetch_object($rs)) {
    $arr[] = $obj;
    }
    echo '{"members":'.json_encode($arr).'}';
    ?>

Then i have an html file that grabs the link to the api.php files and displays all of the entries in a list, I just cant get it to work on cross domain.

showjson.html

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>jQuery</title>
    </style>
    </head>
    <body>
    <div id="msg">
    <table id="userdata" border="1">
    <thead>
    <th>Id</th>
    <th>Product</th>
    <th>Price</th>
    </thead>
    <tbody></tbody>
    </table>
    </div>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
    </script>
    <script type="text/javascript">

    $(document).ready(function(){
    var url="api.php";
    $("#userdata tbody").html("");
    $.getJSON(url,function(data){
    $.each(data.members, function(i,user){
    var tblRow =
    "<tr>"
    +"<td>"+user.postID+"</td>"
    +"<td>"+user.postProduct+"</td>"
    +"<td>$"+user.postDollar+"."+user.postCents+"</td>"
    +"</tr>" ;
    $(tblRow).appendTo("#userdata tbody");
    });
    });
    });

    </script>
    </body>
    </html>

Any help would be greatly appreciated. I apologize for the sloppy wording and code this is my first post on stack overflow :p

3
  • You can use pure AJAX if you can customize the header code on the outside server, which it sounds like you can in this case - look up the term "Cross Origin Resource Sharing (CORS)" Commented Aug 12, 2014 at 21:22
  • thanks @katana314, this seems like an ideal solution for this project, how exactly would I go about writing this in all ajax? Commented Aug 12, 2014 at 22:27
  • I don't know how you would write CORS code outside of ajax. Did you look up the term at all? I'm sorry to dodge the question, but there are numerous tech blogs that could explain the concept far better than a brief SO answer. Commented Aug 13, 2014 at 13:10

1 Answer 1

1

I dont know if i understand your question 100%, but in this type of situation, isnt a good method to generate a php file on the server that runs the javascript to? Just include the remote file with php, call it the same, api.php, and use ajax on that file and pass the parameters on. I have used this method several times.

This is not an actual answer to the question, but i think it is the correct answer in most circumstances.

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

Comments

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.