MsNichols
MsNichols

Reputation: 1133

Can't Post using AJAX in Intel XDK

I've included the required files:

But can't seem to get my ajax post to submit. It seems like the event is being stopped before the post. I am developing an application using IntelXDK and there isn't a lot of details on line how to make AJAX post work... Not sure what I'm missing. Any ideas?

My ajax:

$.ajax({
    url : 'http://xx.xxx.xx.x/check_access.php',
    type: 'POST',
    data : {'thisusername':thisusername, 'thispassword':thispassword},
    dataType: 'Text',
    success: function(data, textStatus, jqXHR)
    {  

        if(data==1){
                  localStorage.setItem("username2", thisusername);
                  location.assign("./community-home.html");
        } else {
               function funcError1() {
                    var iframe5 = document.createElement("IFRAME");
                    iframe5.setAttribute("src", 'data:text/plain,');
                    document.documentElement.appendChild(iframe5);
                    window.frames[0].window.alert('Username or Password does not exist.');
                    iframe5.parentNode.removeChild(iframe5);
                }
                funcError1();
        }
    },
    error: function(){
            //$("#user-result").html("Username available!");
            function funcError() {
                var iframe4 = document.createElement("IFRAME");
                iframe4.setAttribute("src", 'data:text/plain,');
                document.documentElement.appendChild(iframe4);
                window.frames[0].window.alert('Something went wrong. Close the application and try again');
                iframe4.parentNode.removeChild(iframe4);
            }
            funcError();

            console.log('There was an error');
    }

});
return false;
}

Upvotes: 1

Views: 2867

Answers (1)

eashtianjr
eashtianjr

Reputation: 521

In order for you to make ajax requests to a foreign website or service within Intel XDK or apps built by XDK, I recommend that you add the xhr.js script in the HEAD element.

For example,

<!DOCTYPE html><!--HTML5 doctype-->
<html>
<head>
    <title>Your New Application</title>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0" />
    <style type="text/css">
        /* Prevent copy paste for all elements except text fields */
        *  { -webkit-user-select:none; -webkit-tap-highlight-color:rgba(255, 255, 255, 0); }
        input, textarea  { -webkit-user-select:text; }
        body { background-color:white; color:black }
    </style>
    <script src='intelxdk.js'></script>
    <script src='cordova.js'></script>
    <script src='xhr.js'></script>
    <script type="text/javascript">
        var onDeviceReady=function(){                             // called when Cordova is ready
           if( window.Cordova && navigator.splashscreen ) {     // Cordova API detected
                navigator.splashscreen.hide() ;                 // hide splash screen
            }
        } ;
        document.addEventListener("deviceready", onDeviceReady, false) ;
    </script>
</head>
<body>
<!-- content goes here-->
    <h2>Hello World</h2>
</body>
</html>

For more information about making ajax requests within Intel XDK, go to https://software.intel.com/en-us/html5/articles/how-to-access-JSON-data-in-HTML5-apps

Upvotes: 1

Related Questions