Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

profile.json into a Javascript Variable

Dr_SommerDr_Sommer Dr. of tender Programing ;) ✭✭

OK, X00, there it is.. ;))

I think this is not a Vanilla thing, but I#ve searched for solution and found nothing that works... so Youre my hope.. ;)

I am making a REAL Chat for Vanilla with Node.js... of course as always with minimum knowledge... )
Cause PHP will not quite work with Node,js (or JS is better in this context) i need to use the profile.json File with Javascript..

How can I put the "http://caba.de/CaBaFoRuM/index.php?p=/profile.json" into an Javascript Variable, so that I can read out the Name of the logged in User??

i tried:

var json = (function () {

  var json = null;

  $.ajax({
      'async': false,
      'global': false,
      'url': "http://caba.de/CaBaFoRuM/index.php?p=/profile.json",
      'dataType': "json",
      'success': function (data) {
          json = data;
      }
  });
  return json; 

})();

And:

$.getJSON("http://caba.de/CaBaFoRuM/index.php?p=/profile.json", function(data) {
alert(data.msg);
});

Nothing above is working... the Code is not 100% correct, but you'll get the Idea...
Appreciate any Help, thx... ;)

Comments

  • R_JR_J Ex-Fanboy Munich Admin

    I would suspect it doesn't work because you're handling the data outside of the success function, but I don't know much about JS.

    The code below should work:

            $.ajax({
                url: gdn.url('/profile.json'),
                dataType: 'json',
                cache: false,
                success: function(profile){
                    if (profile) {
    // maybe you have to uncomment the next line
    //                    profile = $.postParseJson(profile).Data;
                        alert(profile.Name);
                    }
                }
            });
    
  • x00x00 MVP
    edited July 2014

    first the person has to already be logged in that context . What is the context this code is running?

    If not you have to target a specific user e.g. http://caba.de/CaBaFoRuM/index.php?p=/profile.json/Klausi

    It is cross domain or are you calling it from the same domain becuase you can't request json cross domain with ajax

    You have to use JSONP cross domain:

    jQuery.getJSON("http://caba.de/CaBaFoRuM/index.php?p=/profile.json/Klausi&callback=?", function(data) {
        console.log(data);
    }).fail(function() {
       console.log( "error" );
    });
    

    see that callback=? that is JSONP

    http://en.wikipedia.org/wiki/JSONP

    It may not work still, you will have to set the config option Garden.AllowJSONP to TRUE.

    grep is your friend.

  • Dr_SommerDr_Sommer Dr. of tender Programing ;) ✭✭

    @ R_J: not working... nothing is happening, when I integrate your Code:

    <script>
      $.ajax({
                url: gdn.url('http://caba.de/CaBaFoRuM/index.php?p=/profile.json'),
                dataType: 'json',
               async: false,
                cache: false,
    
                success: function(profile){
    
                    if (profile) {
    // maybe you have to uncomment the next line
                     profile = $.postParseJson(profile).Data;
                        alert(profile);
                        document.write(profile);
                        document.write("docwrite");
                    }
                }
            });    
    
    </script>   
    

    I also tried to uncomment the postParseJson line and also tried to add: " async: false," no luck...
    Maybe the problem lies in the URL??

    @X00:

    The Chat is on the same Domain... no cross Domain.. ;)
    The Chat is stolen from here:
    http://socket.io/get-started/chat/
    Absolutely fantastic, fast and easy... ;)

    I just want to check who is logged in the Forum, then take the Username also to be the Chatname... that simple, but that hard... ;)

    I'll tripplecheck all your Code and links... thats just a quick answer.... thanks for helping... ;)

  • Dr_SommerDr_Sommer Dr. of tender Programing ;) ✭✭

    p.s.: working example (Alpha) here:
    http://caba.de:3000/

  • You shouldn't just check for success you should check for failure too.

    If people aren't logged in there is no point supplying them the chat in the first place. Serve the chat conditional of being logged in. You needn't do this on the client side.

    grep is your friend.

  • Dr_SommerDr_Sommer Dr. of tender Programing ;) ✭✭

    Hmm.. thats what I'm trying to do... to check if the User is logged in and then to enable the chat or to point to the Login... not shure how to do that serverside with node.js...

    I finally checked the Console after running the Script:
    "NetworkError: 404 Not Found - http://www.caba.de/CaBaFoRuM/index.php?p=/profile.json"

    When I put the URL into the Browser, it downloads a "index.php" File with Json Data in it...
    This must be it, but no clue how to fix this...

  • x00x00 MVP
    edited July 2014

    When I put the URL into the Browser, it downloads a "index.php" File with Json Data in it...

    This must be it, but no clue how to fix this...

    that is normally json, if you get a json plugin for your browser it will format it nicely in your browser.

    I need to see the actual example to see what you are doing wrong.

    You don't really want to verify the identity on the client side either, becuase it is no kind of barrier. it is chat basically open any you just want to assign the usernames? Or do you wan t secured system.

    grep is your friend.

  • Dr_SommerDr_Sommer Dr. of tender Programing ;) ✭✭

    ARGHH!!! I think, thats all Bull**t what I'm doing... ;)))

    @X00: In long Term I would like to build a secured Chat, but now I'm still learning... so security is no problem at the moment... ;)

    I Finally made it half way to work:
    1. if I call http://caba.de/CaBaCHaT/index2.html it works and I get the Username
    2. BUT if I call http://caba.de:3000/ I get an 404 not found error...

    Here is the complete Index2.html Code:

    <!doctype html>
    <html>
          <head>
                <title>CaBaCHaT</title>
                <script src="http://code.jquery.com/jquery-git2.min.js"></script>
           </head>
    <body>
           test body
     </body>
    
    
    <script type="text/javascript">
    $(document).ready(function(){
    var url='http://caba.de/CaBaFoRuM/index.php?p=/profile.json';
           $.getJSON(url,function(CaBajson){
                  alert(CaBajson.Profile.Name);
           });      
    });
    </script>
    </html>
    

    As written above, its a part of the Socket.io Chat example:
    http://socket.io/get-started/chat/

    And here my index.js

        var app = require('express')();
        var http = require('http').Server(app);
        var io = require('socket.io')(http);
    
        app.get('/', function(req, res){
          res.sendfile('index2.html');
        });
    
    io.on('connection', function(socket){
      socket.on('chat message', function(msg){
        io.emit('chat message','CaBaNauT: '+ msg);
      });
    });
    
    http.listen(3000, function(){
      console.log('listening on *:3000');
    });
    

    But as you say, I think I'm all wrong... taught its an easy way to do it... ;)

    First I just want an easy way to connect the Chat to the Users... maybe there is another way? Have to reconsider.... ;)

  • Dr_SommerDr_Sommer Dr. of tender Programing ;) ✭✭

    p.s.: the originally index.html for the chat is:

    <!doctype html>
    <html>
      <head>
            <script src="https://cdn.socket.io/socket.io-1.0.6.js"></script>
            <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
    
    
    
        <title>CaBaCHaT</title>
        <style>
          * { margin: 0; padding: 0; box-sizing: border-box; }
          body { font: 13px Helvetica, Arial; }
          form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
          form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
          form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
          #messages { list-style-type: none; margin: 0; padding: 0; }
          #messages li { padding: 5px 10px; }
          #messages li:nth-child(odd) { background: #eee; }
        </style>
    
    
      </head>
      <body>
    
    
        <ul id="messages"></ul>
        <form action="">
            <input id="m" autocomplete="off" /><button>Send</button>
         </form>
    
      </body>
    
     <script>
          var socket = io();
          $('form').submit(function(){
            socket.emit('chat message', $('#m').val()+' <<<<<<<' );
            $('#m').val('');
            return false;
          });
          socket.on('chat message', function(msg){
            $('#messages').append($('<li>').text(msg));
          });
    
    function clear_div() {
        document.getElementById("messages").innerHTML = "";
    }
    </script>
    

  • R_JR_J Ex-Fanboy Munich Admin

    I've stumbled upon something like that before, but I cannot remember what the situation was...

    But I guess the solution will be to eliminate any "www." in your caba.de links. "www.caba.de" and "caba.de" is not the same and your forum is at "caba.de" and so the user is logged in at caba.de, not www.caba.de

  • in the context where you are calling the json is there a vanilla cookie. You can only do session operation if the coookie covers that part of the site. You can use the web developer plugin of fire to check, or the normal inspect of firefox and chrome.

    grep is your friend.

  • Dr_SommerDr_Sommer Dr. of tender Programing ;) ✭✭
    edited July 2014

    @Rj: Tried all combinations with and without WWW, HTTP... not working...

    It's allways the same... when I call "http://caba.de/CaBaCHaT/index2.html" it works and I get the User name but if I call "http://caba.de:3000" I get an 404 not found error...

    But there is no use to call "http://caba.de/CaBaCHaT/index2.html", cause the chat is only working with "http://caba.de:3000"

    @x00: Maybe that could be it...

    when calling "http://caba.de:3000" i get:

    and when I call "http://caba.de/CaBaCHaT/index2.html"

    The Chat folder is OUTSIDE the Forum Folder on the same Level... but i tried it also inside, same problems..

    Is there a way to call the JSON File over a port?? For example "http://caba.de:3000/CaBaCHaT/index2.html" ??
    Or how can I "set" the Cookie??

    As you can read I'm now trying ANYTHING... ;))))))

  • Dr_SommerDr_Sommer Dr. of tender Programing ;) ✭✭

    YESSSSSS!!!!
    Found another, WORKING solution... B) :# :#

    First Tests are promising... the Usernames are displayed and the speed is enormous!!!!

    I'll make some clean up and modifications and then post it for all of you if somebody wants to make a Plugin of it, cause I'm still a noob and have no clue (yet) how to do it... :# ;)

    But anyhow a BiiiiiiiiiiiiG THANX to x00 and R_j for helping... ;)

  • R_JR_J Ex-Fanboy Munich Admin

    WebSocket is a "new" HTML5 feature and the support is not too good by now: http://caniuse.com/#search=websocket
    I've seen some implementations with a fallback to flash :\ and there is an implementation with Ratchet (a PHP WebSocket library). If someone is doing a plugin, I would prefer PHP since there is no need for Node.js on a Vanilla server and PHP is already there...

  • Dr_SommerDr_Sommer Dr. of tender Programing ;) ✭✭
    edited July 2014

    My solution is a combination of PHP and Node.js...
    I decided to use Node.js, cause it's damn fast!!! Before then I used Ajax Chat and hat performance problems on my small VServer...

    My Prealpha working on all major Browsers... with klick sound... yeah.. :D:smiley::smiley: (only on IE no klick)

  • R_JR_J Ex-Fanboy Munich Admin

    What makes that chat fast isn't node.js, it's WebSocket which makes it fast. You can read about it here: http://www.html5rocks.com/en/tutorials/websockets/basics/
    You'll need js on the client side to do the live update of the page, but the language of the server is not important.

  • R_JR_J Ex-Fanboy Munich Admin

    Don't know about resource consumption, but having a forum using WebSocket instead of AJAX could be quite interesting :)

  • Dr_SommerDr_Sommer Dr. of tender Programing ;) ✭✭

    Yes, thx for clarification... this is all new to me and aaalll of it ONE big mystery..... ;)
    A Forum using Websocket?? Arghh... don't put such funny Ideas in my small head!! I'll might try that...... :smiley::smiley:

  • R_JR_J Ex-Fanboy Munich Admin

    At least the notifications could be pimped that way ;)

Sign In or Register to comment.