4 Replies Latest reply on Oct 8, 2017 4:32 PM by kerrishotts

    Using JavaScript/jQuery/Ajax

    phonegapdev_g54367927

      Hello.

      I have a short question, hope someone can help me.

       

      I'm trying to build a PhoneGap App for Android.

      I'm using Javascript, and also jQuery & Ajax. I use Mamp to develop and test the files on my local machine. When I run my index.html in Mamp, it works. When I run it on my local machine without Mamp, it does not work.

       

      So the question is - will my project run on a PhoneGap App for Android?

      And if so, how does this work? Does PhoneGap Apps have some kind of a integrated Mamp (or something like that), that emulates a webserver?

      I have tried to google it, but can not find a answer.

       

      I'm sorry, but I don't have a Wifi at home right now, so I got to wait some weeks before I can deploy my test on Android over PhoneGap.

      I just would like to know right now, if it works or not - and when it works, how it can work.

       

      Thanks!

        • 1. Re: Using JavaScript/jQuery/Ajax
          zacharyr30798184 Level 1

          If you are linking externally to the jquery source then you probably need to update your content security policy in the head section of your html document to whitelist those domains. You may also need to do that in the manifest of your project as well depending on your setup.

           

          I recommend just copying the jquery source code into your project js file and use it like that. It will also ensure that your jquery code runs even when the user does not have a valid network connection, which I would highly recommend.

          • 2. Re: Using JavaScript/jQuery/Ajax
            ev97151377

            Thank you for your help!

            So instead of using the jquery code directly in my html file, you would recommend to copy my jquery code into a new js file and link to this file from my html file, right?

            Could you, maybe, make a very small example for me?

            I'm new to javascript and jquery.

            I use javascript to load content from a mysql database. I use this because I don't wanna reload the whole page everytime just to load new data from the database.

            So right now I have a javascript function in the head section of my html file to change values by using:

            document.getElementById("idhere").innerHTML

            And I call this function from my body section of my html file.

             

             

            You would help me so much if you could explain what I have to do or maybe make a very small example!

             

             

            Thanks for your help!

            • 3. Re: Using JavaScript/jQuery/Ajax
              zacharyr30798184 Level 1

              Well, instead of LINKING to the jquery code externally you just copy the js file and put it in your project folder and link to that local file instead. The actual AJAX code you run will be exactly the same as before. You wouldn't want to copy all the jquery source code to your html header section as it would be too long and awkward for editing. Keep it in a separate file.

              • 4. Re: Using JavaScript/jQuery/Ajax
                kerrishotts Adobe Employee

                I've been traveling as of late, so didn't catch up to this question until now. To your original questions:

                 

                I'm trying to build a PhoneGap App for Android.

                I'm using Javascript, and also jQuery & Ajax.

                Is there a reason you're using jQuery, and what are you using it for? You can accomplish Ajax without (either with XHR2 or the Fetch api), and if you're trying to use it for UI, there are much better options out there (Ionic, Framework7, Onsen, etc.).

                 

                I use Mamp to develop and test the files on my local machine. When I run my index.html in Mamp, it works. When I run it on my local machine without Mamp, it does not work.

                So the question is - will my project run on a PhoneGap App for Android?

                 

                Maybe. It depends on how you've built your app. From the sounds of it, you've got a frontend (JS & UI) that makes XHR calls back to your backend (server) for data. You can make this work well with PhoneGap, but you'd have to have a backend that's exposed to the Internet -- and all the security risks that comes with. You might be better off with something like Firebase or some other BaaS (backend as a service) provider.

                 

                And if so, how does this work? Does PhoneGap Apps have some kind of a integrated Mamp (or something like that), that emulates a webserver?

                 

                PhoneGap itself is not a server, and has no server capabilities. If you're using the MAMP stack solely to serve as a local database of sorts, you can use various SQLite plugins to get a local database. If you need to share data between users, you have to have a backend that all your clients will connect to (hence the above recommendation for a BaaS).

                 

                Be sure to read up on the Cordova / PhoneGap docs, especially regarding whitelisting, since it sounds like you'll need to configure your app to communicate over the network.

                 

                I'm sorry, but I don't have a Wifi at home right now, so I got to wait some weeks before I can deploy my test on Android over PhoneGap.

                 

                You can deploy your app via USB using the CLI. If your dev machine has a wifi adapter it should also be able to generate an adhoc network to which your Android device should be able to connect.

                 

                I just would like to know right now, if it works or not - and when it works, how it can work.

                 

                Unfortunately, there's not really enough information here to give you a certain answer. It might help to know more about what kind of app you're trying to build, and what architecture you're using.