Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2013
    Location
    Netherlands
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool cannot call 2 Json function on 1 page why?

    Hi all,
    I'm a bit lost and try to understand what I'm doing wrong.
    I have 2 functions calling Json Queries that I want to use on different place ( InnerHtml) place on my page.

    If I used one or the other only it works but applying both on the page it' s broken.

    The function are using Dojo 1.9.x Xhr.Get
    there are quite simple.


    <script type="text/javascript" >

    function myBook(){

    require(["dojo/_base/xhr", "dojo/dom", "dojo/_base/array", "dojo/domReady!"],
    function(xhr, dom, arrayUtil) {

    // Keep hold of the container node
    var containerNode = dom.byId("LivresPane");

    // Using xhr.get, as we simply want to retrieve information
    xhr.get({
    // The URL of the request
    url: "livres.php",
    preventCache: true,

    // Handle the result as JSON data
    handleAs: "json",
    // The success handler
    load: function(jsonData) {
    // Create a local var to append content to
    var Book="";


    // For every news item we received...
    arrayUtil.forEach(jsonData.items, function(items) {
    // Build data from the JSON
    Book += "<h2>" + items.Title + "</h2><br>";
    Book += "<p>Auteur: " + items.Author + "&nbsp;";
    Book += "<p>ISBN:" + items.ISBN + "</p>";
    Book += "<p>Web:" + items.WWW + "</p>";


    });


    // Set the content of the news node
    containerNode.innerHTML = Book;
    },
    // The error handler
    error: function() {
    containerNode.innerHTML ="Rubrique non disponible."
    }
    });

    });

    }

    function myRub(rub){

    require(["dojo/_base/xhr", "dojo/dom", "dojo/_base/array", "dojo/domReady!"],
    function(xhr, dom, arrayUtil) {

    // Keep hold of the container node
    var containerNode = dom.byId("MainContainer");

    // Using xhr.get, as we simply want to retrieve information
    xhr.get({
    // The URL of the request
    url: "inc/rub.php",
    preventCache: true,
    content: {
    //add rub parameter once find out how to
    Rub:rub; },
    // Handle the result as JSON data
    handleAs: "json",
    // The success handler
    load: function(jsonData) {
    // Create a local var to append content to
    var content=""


    // For every news item we received...
    arrayUtil.forEach(jsonData.items, function(items) {
    // Build data from the JSON
    content += "<h2>" + items.TitleContent + "</h2>";
    content += "<p>Auteur: " + items.AuthorContent + "&nbsp;";
    content += "&nbsp; Date: " + items.DateContent + "&nbsp;";
    content +="&nbsp;Rubrique: " +items.NameRubriques +"</p>" ;
    content += "<p>" + items.TextContent + "</p>";


    });


    // Set the content of the news node
    containerNode.innerHTML = content;
    },
    // The error handler
    error: function() {
    containerNode.innerHTML ="Rubrique non disponible."
    }
    });

    });

    }


    Each works if they are unique on the page but page fails to load if both are define in page ("Head" tag) .

    Any suggestion or advise what my mistake can be?

    Thanks

    Paul

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,462
    Thanks
    0
    Thanked 633 Times in 623 Posts
    1. Wrap each separate script inside an anonymous self executing function. That will take any variables they use out of the global namespace and prevent interference with other scripts in the same page.

    2. JavaScript belongs at the bottom of the page just before the </body> - that allows you to get rid of any code that waits for the page content to finish loading - which ooften clashes with other scripts trying to use the same code.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    is LivresPane inside MainContainer ?
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •