6 Replies Latest reply on Mar 16, 2017 6:20 AM by ThamodharanK555

    Ajax Request fails often

    ThamodharanK555

      Hello All,

       

      I am building mobile apps with Cordova CLI and using AJAX request for fetching information from the server.

      The request often takes me to the error part of the AJAX. It happens in dedicated servers also.

       

      Here is the code,

       

      $.ajax({

                 url: "http://example.com/api/api.php?apicall=getData",

                 data: data,

                 type: "POST",

                 async: false,

                 success: function(data) {

                 //Success code

                 },

                 error: function(error){

                 //Error part. This is often called.

                 }

                 });

       

      Please help me to get rid of this. Thanks in advance.

        • 1. Re: Ajax Request fails often
          VectorP Level 4

          The request often takes me to the error part of the AJAX

          But not always? In such cases, it would be interesting to know what separates those cases.

          Also, it would be interesting to know:

          - what the error message is

          - whether or not it occurs in specific OS (versions)

          - whether or not you are certain that the internet connection is up at the time of the request

           

          Causes could be:

          . incorrect whitelisting for certain external sources

          - failed to wait for the deviceready event to fire

          - CSP related blocks

          - connection lost

          1 person found this helpful
          • 2. Re: Ajax Request fails often
            ThamodharanK555 Level 1

            Hello VectorP

            My internet connection is always up in the phone.

            It occurs commonly in the iOS and rarely in android.

            I have allow the origin to access my server in config file.

             

            It throws the error "Network error".

             

            Thanks in advance.

            • 3. Re: Ajax Request fails often
              ThamodharanK555 Level 1

              Hello All,

               

               

              We have facing the following error frequently while making ajax request to the server from our app.

               

              {"readyState":0,"status":0,"statusText:" "NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://example.com/*.php'."}

               

              Thanks in advance.

              • 4. Re: Ajax Request fails often
                kerrishotts Adobe Community Professional

                This can be a result of CORS (Cross-origin resource sharing) not being properly configured on your backend. It might also be an issue with your Content Security Policy meta tag.

                 

                Please share the code you're using for network requests as well as plugins installed on your project (and version #s).

                • 5. Re: Ajax Request fails often
                  ThamodharanK555 Level 1

                  Hello,

                   

                  Thanks for your response.

                  Here is the code,

                   

                  $.ajax({

                             url: "http://example.com/api/api.php?apicall=getData",

                             data: data,

                             type: "POST",

                             async: false,

                             success: function(data) {

                             //Success code

                             },

                             error: function(error){

                             //Error part. This is often called.

                             }

                             });

                   

                  Here is the php code

                   

                   

                  <?php

                    include "connections.php";

                   

                    $select_tag = "select * from table";

                   

                    $exe_tag =mysql_query($select_tag);

                    $Count= mysql_num_rows($exe_tag);

                    if($Count > 0)

                    {

                    while($res_tag = mysql_fetch_array($exe_tag)){

                    $tag_info[] = $res_tag;

                    }

                    echo json_encode($tag_info);

                    }

                    else

                    {

                    echo '0';

                    }

                   

                  ?>

                   

                   

                  Do you mean to add header("Access-Control-Allow-Origin: *") to the php file.

                   

                  I have added <access origin="*" /> to the config file. Still the error persists.

                   

                  Let me know your thoughts please.

                   

                  Thanks in advance.

                  • 6. Re: Ajax Request fails often
                    ThamodharanK555 Level 1

                    Hi,

                     

                    I have solved this problem somehow.

                     

                    These are the things I did:

                     

                    1. Put header in all php files to access origin.

                    2. Decrease the number of requests made by the app to the server to its minimum.

                     

                    After doing the second, I noticed that the error does not occur often. But still getting failed to access the server in rare cases.

                     

                    Thanks for your response.