4 Replies Latest reply on Mar 22, 2016 12:20 PM by wtaveras

    Problem with $.ajax calls

    wtaveras

      Hi everyone,


      I'm new using cordova and I'm having a problem while trying to implement a simple post request. There is no error, alert, nothing, just not working.

       

      This one isn't working:

       

      $.ajax({

                      "method": "GET",

                      "url": 'http://ip_address/test_post.php',

                      dataType: 'html',

                      error: function (jqXHR, textStatus, errorThrown)

                        {

                          console.log('new textStatus=' + textStatus + ' errorThrown=' + errorThrown);

                      },

                      success: function (response) {

                         console.log(response);

                      }

              });

       

      But this one is working (the only difference is that the file test_post.php is in my local www folder):

       

      $.ajax({

                      "method": "GET",

                      "url": 'test_post.php',

                      dataType: 'html',

                      error: function (jqXHR, textStatus, errorThrown)

                        {

                          console.log('new textStatus=' + textStatus + ' errorThrown=' + errorThrown);

                      },

                      success: function (response) {

                         console.log(response);

                      }

              });

       

      I already changed the configuration in my res/xml/config.xml

       

      <access origin="*"/>

       

      Can anyone help me out? If you are an expert please explain like I DON'T KNOW ANYTHING. Thanks

        • 1. Re: Problem with $.ajax calls
          kerrishotts Adobe Community Professional

          No offense, but your second example can't be working. PhoneGap is not a PHP server, so it wouldn't get much of use. What you're probably getting instead is the source code of the PHP script, which is probably not what you want.

           

          Also, don't change res/xml/config.xml -- that's not yours to mess with. Change your project's config.xml instead (just above your www directory).

           

          How are you debugging? Are you using Chrome/Safari to view the JavaScript console? If so, I bet that there are messages being logged there.

           

          Have you added (or do you have) a CSP meta tag (Content-Security-Policy)? If you haven't, you need to add one. If you have one, show us what it is (it's in your index.html).

           

          Also make sure that your device can reach your server. Try the URL in a web browser to verify reachability. If it can't connect, make sure that the server isn't blocking your connections. (If you're using your dev machine, your device needs to be on the same physical network.)

          • 2. Re: Problem with $.ajax calls
            wtaveras Level 1

            Correct: You're right about the first point. Hehehe.


            I'm using a console where I get the logs (console.log), I can see everything, but not any result from the request, except if I use the second.
            So my problem is I can't make requests using http:// in the url. All the sites I tried are available and working. I do the same call using another application, not in cordova, and it works perfectly.

             

            I do have the Content-Security-Policy meta tag.


            And about the last thing, I'm using my dev machine and both are on the same physical network.

             

            Thanks for your response.

            • 3. Re: Problem with $.ajax calls
              kerrishotts Adobe Community Professional

              1. Make sure you can access your dev machine from a browser on the device (like Safari or Chrome). If you can't make sure you are letting connection requests through your firewall.

              2. Post your CSP meta tag so we can validate it.

              • 4. Re: Problem with $.ajax calls
                wtaveras Level 1

                Hi,

                 

                Yes. I can access the url from the browser on the device.


                This is the CSP content:

                 

                <meta http-equiv=“Content-Security-Policy” content=“default-src ‘self’ data: gap” https://ssl.gstatic.com; style-src ‘self’ ‘unsafe-inline’; media-src *”>

                    <meta http-equiv=“Content-Security-Policy” contnet=“default-src ‘self’ http:”>