![]() |
![]() OCAU News - Wiki - QuickLinks - Pix - Sponsors |
|
|||||||
| Notices |
|
Sign up for a free OCAU account and this ad will go away! Search our forums with Google: |
![]() |
|
|
Thread Tools |
|
|
#1 |
|
Member
Join Date: May 2004
Location: Banksia Park, SA
Posts: 434
|
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:
Code:
{"a":1,"b":2,"c":3,"d":4,"e":5}
error Object { readyState=0, status=0, statusText="error"}
__________________
Over $10,000 worth bought, $15,000 sold through forums |
|
|
|
| Join OCAU to remove this ad! |
|
|
#2 |
|
Member
Join Date: Jun 2006
Posts: 472
|
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 |
|
|
|
|
|
#3 |
|
Member
Join Date: May 2004
Location: Banksia Park, SA
Posts: 434
|
Those URL's aren't valid...
__________________
Over $10,000 worth bought, $15,000 sold through forums |
|
|
|
|
|
#4 |
|
Member
Join Date: Sep 2008
Location: in the computer
Posts: 621
|
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)
}
})
});
});
|
|
|
|
![]() |
| Bookmarks |
|
Sign up for a free OCAU account and this ad will go away! |
| Thread Tools | |
|
|