skip to Main Content

I am trying to do an ajax call. its just not getting any feedback from the server. can someone help look at my code and advice me on how to go about it. i have looked up lots of answers here but cant seem to get it to work

Jquery Code

here is the jquery code that performs the ajax call

 $(document).ready(function(){
    $('#loadAllProducts').html('Loading Product...<i class="fa fa-spinner fa-spin"></i>').fadeIn(); 
        
             $.ajax({
                    type : 'post',
                    url : 'https://spg.ng/farmtransactpro/api/loadAllProductsOnLoad.php', 
                    //data :  dataString,
                    crossDomain: true,
                    success : function(result){
                    $('#loadAllProducts').html(result).fadeIn();    
                    
                    }
                    
                });
                
                
        });
        

loadAllProductsOnLoad.php

this is the api content

<?php
    header('Content-Type: application/json');
    header("Access-Control-Allow-Origin: *");
    
     echo 'hi...am bringing feedback';
    ?>

config.xml

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.farmtransact" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cdv="http://cordova.apache.org/ns/1.0">

    <name>Farmtransact</name>

  <description>
      FarmTransact is an online marketplace which consists of all kinds of agricultural products and agricultural services
  </description>

 <author email="" href="">
        Chike Okoli
    </author>
    <content src="index.html" />
    <preference name="DisallowOverscroll" value="true" />
    <preference name="android-minSdkVersion" value="14" />
    <access origin="https://spg.ng/farmtransactpro/api/" />
    
    
    <plugin name="cordova-plugin-whitelist" source="npm" spec="1.1.0" />
        <allow-navigation href="*" />
        <allow-intent href="*" />
        <access origin="*" />
    
    <platform name="android">
        <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application">
            <application android:usesCleartextTraffic="true" />
        </edit-config>
    
    
        <icon density="ldpi" src="www/res/icons/android/icon-36-ldpi.png" />
        <icon density="mdpi" src="www/res/icons/android/icon-48-mdpi.png" />
        <icon density="hdpi" src="www/res/icons/android/icon-72-hdpi.png" />
        <icon density="xhdpi" src="www/res/icons/android/icon-96-xhdpi.png" />
        <icon density="xxhdpi" src="www/res/icons/android/icon-128-xxhdpi.png" />
        <icon density="xxxhdpi" src="www/res/icons/android/icon-192-xxxhdpi.png" />
        
        <splash density="land-ldpi" src="www/res/screens/android/screen-ldpi-landscape.png" />
        <splash density="land-mdpi" src="www/res/screens/android/screen-mdpi-landscape.png" />
        <splash density="land-hdpi" src="www/res/screens/android/screen-hdpi-landscape.png" />
        <splash density="land-xhdpi" src="www/res/screens/android/screen-xhdpi-landscape.png" />
        
        
        <splash density="port-ldpi" src="www/res/screens/android/screen-ldpi-portrait.png" />
        <splash density="port-mdpi" src="www/res/screens/android/screen-mdpi-portrait.png" />
        <splash density="port-hdpi" src="www/res/screens/android/screen-hdpi-portraite.png" />
        <splash density="port-xhdpi" src="www/res/screens/android/screen-xhdpi-portrait.png" />
        <splash src="splash.png" />
    </platform>
    <access origin="https://spg.ng/farmtransactpro/api/" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <platform name="android">
        <allow-intent href="market:*" />
    </platform>
    <platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
    </platform>
</widget>

2

Answers


  1. As I said in the comment, you might have a problem regarding: Content-Security-Policy

    I built some apps with Cordova, but I’m not very advanced. This might help you though, so give it a try:

    -> in your main index.html file, add this in the head section:

    <meta http-equiv="Content-Security-Policy" content="connect-src 'self' https://spg.ng/farmtransactpro/api/">
    

    Does that work?

    See here a complete guide for Cordova Content-Security-Policy – docs

    Login or Signup to reply.
  2. This is not triggering the success handler.

    Add an error handler, and log the response to console.

    You will see it contains statusText: "parsererror" – and that is a result of pretending the response was of Content-Type: application/json, but then sending data that is not actually valid JSON.

    So, solution: Make your PHP script send actual valid JSON.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search