Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-05-2012, 01:28 PM   PM User | #1
MeltingDog
New to the CF scene

 
Join Date: May 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
MeltingDog is an unknown quantity at this point
JSON with PHP and JQUERY - almost there!

Hi all,

I am pretty much learning JSON from scratch and am having some problems finding good resources. I was wondering if anyone here could just help me push this over the line.

Basically my situation is:

I have a server with a CMS. I want information from the CMS to appear on several other sites.

On a PHP Snippet on the 1st server I have:

Code:
header("content-type: application/json");
header("Access-Control-Allow-Origin", "*")
header("Access-Control-Request-Method", "GET")
header("content-type", "application/x-javascript")

$item1 = 'hello';


$item1 = json_encode($users);
echo $_GET['jasoncallback'] . '(' . $item1 . ')';
On the 2nd .html page I have:

Code:
 $.ajax({ url:"http://localhost/mbff_cms?jsoncallback=?", 
                     dataType: "jsonp", 
                     data: {jsoncallback:'jsoncallback'},
                     success: function(msg) {
                     console.log('server answered:', msg);
                     // do something with received message
                     }
                     });
The result I get in the console is:

GET http://localhost/mbff_cms/?jsoncallb...=1338898608228 jquery-1.7.2.js:8123

Obviously I just want to see the word 'Hello' in the console. I just want to pass the PHP variable over to the .html page and display it using jquery.

Can anyone help me get this over the line?

Thanks heaps!
MeltingDog is offline   Reply With Quote
Old 06-05-2012, 07:29 PM   PM User | #2
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 809
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
i think you are not close .
You are fetching from the server,
with your jqueery ajax, some text
that has been formatted as jsonp,
if you don't fully understand what
i just said then, you are really
not close at all.

Ajax usually only works
in the same domain while
jsonp can be called from
a different domain .

No good reason i can
think of for mixing the
two as you have ?

Last edited by DaveyErwin; 06-07-2012 at 03:26 AM..
DaveyErwin is offline   Reply With Quote
Old 06-06-2012, 02:11 AM   PM User | #3
MeltingDog
New to the CF scene

 
Join Date: May 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
MeltingDog is an unknown quantity at this point
Thanks Davey,

Would I be better replacing the Ajax with something like:

$.getJSON("http://localhost/mbff_cms?jsoncallback=?",
function(data) {
console.log(data)
});
MeltingDog is offline   Reply With Quote
Old 06-06-2012, 02:57 AM   PM User | #4
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 809
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
this works on the same server ...

Code:
<!doctype html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<title></title>
</head>
<body>
<script> 
req = new XMLHttpRequest;
req.open("GET","json.php",false);
req.send(null);
alert(JSON.parse(req.responseText).a);// alerts "I'm a!"

</script> 
</body>
</html>
no need to wrap the json
with a function call ...

json.php ...

PHP Code:
<?php
class Foo
{
    public 
$a "I'm a!";
    public 
$b "I'm b!";
    public 
$c;
}
$obj = new Foo();
echo 
json_encode($obj);

?>
okay here is another way
this will work on remote server ...

Code:
<!doctype html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<title></title>
</head>
<body>
<script>
function callback(resp){alert(resp.a)}// alerts "I'm a!"
</script>
<script src = "json.php"></script> 
</body>
</html>
the json.php

PHP Code:
<?php
class Foo
{
    public 
$a "I'm a!";
    public 
$b "I'm b!";
    public 
$c;
}
$obj = new Foo();
echo 
"callback(".json_encode($obj).")";

?>

Last edited by DaveyErwin; 06-06-2012 at 03:23 AM..
DaveyErwin is offline   Reply With Quote
Reply

Bookmarks

Tags
jquery, json, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 +1. The time now is 10:14 AM.


Advertisement
Log in to turn off these ads.