Search code examples
angularjscordovaionic-frameworkcordova-plugins

Error 404 after $http.post Ionic - Android build


We are developing a mobile app using the Ionic framework. Here are the config.xml and index.html files:

config.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<widget id="com.ionicframework.manageyourmatch988887" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
  <name>manageyourmatch</name>
  <description>
        An Ionic Framework and Cordova project.
    </description>
  <author email="hi@ionicframework" href="http://ionicframework.com/">
      Ionic Framework Team
    </author>
  <content src="index.html"/>
  <access origin="*"/>

index.html

        <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">

When we perform a post request in the loginCtrl.js on the device we get the 404 error. What are we missing? We allowed all sources through the whitelist using the access tag and the CSP tag.

loginCtrl - http post

$http.post(APP_CONFIG.serverUrl, // server url for connetion
            {action : "login", cell_num : $scope.model.telephone, password : $scope.model.password} // data passed by json post
            ).then(
            function(response) { // if i recive a response from the server
                console.log(response.data);

                if(response.data.status == "success"){ // if the server accepts my login
                    // show toast [native] or an alertPopup [all platforms]
                    if(typeof window.plugins !== "undefined")
                        window.plugins.toast.showLongBottom('Response: ' + response.data.status);
                    else
                        $ionicPopup.alert({title: 'Server response', template: ''+response.data.status});

                    // test the vibration [HTML5 all platforms]
                    //navigator.vibrate(1000);

                    // Update localStorage
                    localStorage.setItem("telephone", $scope.model.telephone);
                    localStorage.setItem("loggedin", "true");
                    localStorage.setItem("session_id", response.data.session_id);

                    // Go to homepage
                    $state.go('home.matches');
                }else{
                    // if the login fail due to bad username and password (or something else...)
                    $ionicPopup.alert({
                            title: 'Error message',
                            template: ''+response.data.error_message
                        });
                }
            }, function(error) { // if something goes wrong
                console.log(JSON.stringify(error));
                $ionicPopup.alert({
                        title: 'Connection failed',
                        template: 'Error: '+JSON.stringify(error)
                    });
            });

Solution

  • This solved the problem:

    <meta http-equiv="Content-Security-Policy" content="default-src *; script-src &apos;self&apos; &apos;unsafe-inline&apos; &apos;unsafe-eval&apos; *; style-src  &apos;self&apos; &apos;unsafe-inline&apos; &apos;unsafe-eval&apos; *">