0

Here's the code:

<!DOCTYPE html> 
<html> 
    <head> 
    <title>User2</title> 
        <meta name="viewport" content="width=device-width, initial-scale=1"> 
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
        <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>        
        <script>

            $(document).ready(function() {                
                $.support.cors = true;
                //Uses Tasty Pie REST APIs
        $("#user2-adduser2").click(function() {data = JSON.stringify({"username":$("#user2-username").val() === ""? null:$("#user2-username").val(), "product":$("#user2-product").val() === "" || $("#user2-product").val() === "Option(s)"? null:$("#user2-product").val()})
               $.ajax({
               url: 'http://xxxxxxdomainxxxxx/api/v1/user2/?format=json',
        type: 'POST',
                           async: false,
                           contentType: 'application/json',
                           data: data,
                           dataType: 'json',
                           processData: false,
                           success: function(data){
                           alert("Done!")
                           //Goto Next Page

                      },

                      error: function(jqXHR, textStatus, errorThrown){
                          alert("Some Error!")
                          }
                     })
                });

            ////For Dynamically loading dropdown options
            $.ajax({
                type: 'GET',
                url: 'http://xxxxxxdomainxxxxx/api/v1/productapp/?format=json',            
                accepts: 'application/json',
                success: function(data){
                    var options = '<option>Option(s)</option>'
                    for(var i = 0; i < data.objects.length; ++i) {
                        var str =  '<option value="/api/v1/productapp/' + data.objects[i].id + '/">' + data.objects[i].productname + '</option>'
                        options=options+str
                    }
                    $('#user2-product').html(options).selectmenu("refresh");
                },
                dataType: "json"
                });
            });// end of document.ready function

        </script>
    </head>
    <body> 

    <div id="user2">
        <h1>User2</h1>
        <div id="user2-username-div">
            username:
            <input  type="text" id="user2-username">
        </div>
        <div id="user2-product-div">
            Product:
            <select  id="user2-product" data-native-menu="false"></select>
        </div>
        <a id="user2-adduser2" href="#" data-role="button">Add User2</a>      
    </div>

    </body>
</html>

The problem exists for android browser alone. All the static contents of the page are loading. However any dynamic content, that is in this case, the options for the drop-down coming from server are not loaded. If you just refresh browser once, everything works fine.

Have tried almost everything today but no luck. Any advise/help is highly appreciated.

ranjjose
  • 2,138
  • 1
  • 24
  • 46
  • Generally you don't want to use `document.ready` with jQuery Mobile pages. That's not to say it's illegal or something, but take a look at the events page of the documentation, there may be an event that works better for you (like `pageinit` maybe): http://jquerymobile.com/demos/1.2.0/docs/api/events.html – Jasper Feb 14 '13 at 18:59
  • @Jasper, Thanks for your reply. I had already tried `pageinit`. I tried the same again but no success. Anyway, looking at [a similar stackoverflow question](http://stackoverflow.com/questions/7544023/how-to-initialize-pages-in-jquery-mobile-pageinit-not-firing) I guess it's a bug. As mentioned in that link, I tried putting script inside the `
    ` and it works! Anyway, I will update here if I get anything around this issue.
    – ranjjose Feb 15 '13 at 09:01
  • To give a background, I was testing a multipage-template from mobile browsers (android) when I saw that only the static contents of 1st page (i.e., the contents of 1st `
    ` with `data-role="page"`) is loaded. The links to the other 'pages' were not working. All the pages of my multipage-template is in one html file. Once refreshed, everything seems working. Now, with putting ajax scripts inside `
    ` of each page, dynamic contents of 1st page works. but links to other pages still don't work. For that still a refresh seems to be required. Will update if I get anything in this regard. Thanx
    – ranjjose Feb 15 '13 at 09:34

1 Answers1

0

OK! Finally I fixed the issue.

The ajax http GET requests were not working because they were by default cached. Hence no request were sent to the server from mobile browsers. Once I put cache=false in $.ajax requests, it's all working. Phew!

In-fact I had two issues. (1) The links to other pages of a multi-page template was not activated and (2) Ajax content was not loading.

Problem (1) got solved when I used data-ajax="false". Problem (2) as explained, when I added cache=false in $.ajax requests. Hope it helps someone, someday. Thanks

ranjjose
  • 2,138
  • 1
  • 24
  • 46