Overclockers Australia Forums
OCAU News - Wiki - QuickLinks - Pix - Sponsors  

Go Back   Overclockers Australia Forums > Software Topics > Programming & Software Development

Notices


Sign up for a free OCAU account and this ad will go away!
Search our forums with Google:
Reply
 
Thread Tools
Old 20th June 2012, 9:17 PM   #1
xdaman Thread Starter
Member
 
xdaman's Avatar
 
Join Date: May 2004
Location: Banksia Park, SA
Posts: 434
Default Help with PHP/JSON/Ajax

Hi all,

Just trying to prototype something for a web app I want to make. Effectively I want the PHP to act as an API that returns json.

A HTML page will be served up that will make a call to the PHP, I want the PHP to return a JSON object for the JavaScript to manage.

Code:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Prototype</title>
</head>
<body>
<button id="test">Test</button>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script>
    $(document).ready(function(){
        $("#test").click(function(){

            $.ajax({
                url: 'http://localhost/index.php',
                type: "POST",
                dataType: "json",
                success: function(data, data2, data3) {
                    console.log("test", data, data2, data3);
                },
                error: function(error) {
                    console.log("error", error)
                }
            })

        });
    });
</script>
</body>
</html>
PHP Code:
<?php
$arr 
= array('a' => 1'b' => 2'c' => 3'd' => 4'e' => 5);

header('Content-Type: text/javascript; charset=utf8');
header('Access-Control-Allow-Origin: http://www.example.com/');
header('Access-Control-Max-Age: 3628800');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');

$jsonjson_encode($arr); 
print_r($json); 

?>
Visiting the PHP shows this:

Code:
{"a":1,"b":2,"c":3,"d":4,"e":5}
Making the call via the AJAX returns a 200 OK but calls the error callback:
error Object { readyState=0, status=0, statusText="error"}
__________________
Over $10,000 worth bought, $15,000 sold through forums
xdaman is offline   Reply With Quote

Join OCAU to remove this ad!
Old 21st June 2012, 1:23 AM   #2
Wako
Member
 
Join Date: Jun 2006
Posts: 472
Default

slapped it on my dev box
worked fine for me.

http://littleman/temp/aj.html
was your ajax page

I only changed it to point to your php code to where it resided on my box
http://littleman/temp/index.php
__________________
CHIPS
Wako is offline   Reply With Quote
Old 21st June 2012, 1:34 PM   #3
xdaman Thread Starter
Member
 
xdaman's Avatar
 
Join Date: May 2004
Location: Banksia Park, SA
Posts: 434
Default

Those URL's aren't valid...
__________________
Over $10,000 worth bought, $15,000 sold through forums
xdaman is offline   Reply With Quote
Old 21st June 2012, 2:23 PM   #4
rm -rp ./Matt*
Member
 
rm -rp ./Matt*'s Avatar
 
Join Date: Sep 2008
Location: in the computer
Posts: 621
Default

your ajax call is wrong, also refer to a newer jquery version, I believe it's up to 1.7, Chrome was spitting errors about this one. I'm not sure about your error code, I haven't had use for those yet. I set the type to GET because you aren't sending any data to php to process you're just grabbing static data.

Code:
$(document).ready(function(){
        $("#test").click(function(){

            $.ajax({
                url: 'http://localhost/index.php',
                type: "GET",
                dataType: "json",
		cache: false,
                success: function(data) {
                    console.log("test", data.a, data.b, data.c);
                },
                error: function(error) {
                    console.log("error", error)
                }
            })
        });
    });
rm -rp ./Matt* is offline   Reply With Quote
Reply

Bookmarks

Sign up for a free OCAU account and this ad will go away!

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +10. The time now is 4:15 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd. -
OCAU is not responsible for the content of individual messages posted by others.
Other content copyright Overclockers Australia.
OCAU is hosted by Internode!