jQuery不打算使用phonegap

时间:2014-12-21 19:42:47

标签: javascript jquery json cordova

此代码适用于不同的浏览器(Chrome,Firefox,Safari),但在PhoneGap中,当我点击按钮“登录”时, logme功能不起作用。

我试图用vclick替换点击, 或者//ajax.gooogleapis.com / ....按文件://ajax.goo ....但它不起作用。

你对这个问题有所了解吗?

感谢

<!DOCTYPE html>
<html>
<head>
<style>
* { font-family: Verdana, Geneva, sans-serif; line-height: 30px }
.title { background:#333; color: white; }
.success { color: #060; font-weight: bold; }
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
var apiURL = "localhost/wordpress/api/";
var noncestring = "get_nonce/?";
var authstring = "user/generate_auth_cookie/?";
var poststring = "posts_auth/create_post/?";
var username, password;
var nonce, cookie;

$('document').ready(function(){
    $('#logme').click(function() {

        jQuery(function($) {
            username = document.forms["logme"].elements["username"].value;
            password = document.forms["logme"].elements["password"].value;
        });

        getNonce("user", "generate_auth_cookie");

        function getNonce(controller, method) { 
            $.getJSON(apiURL + noncestring + "controller=" + controller + "&method=" + method, function(data) {

                var items = [];
                $.each(data, function(key, val) {
                    if (key == "nonce") {
                        nonce = val;
                        $('.status').append("<br>Nonce acquired for controller '" + controller + "', method '" + method + "': " + val);

                        // Add additional methods here. Could make this a switch statement. 
                        if (method == "generate_auth_cookie") 
                            createAuthCookie();

                        if (method == "create_post")
                            createPost();

                        getid();

                    }
                });
            });
        }


        function createAuthCookie() {

            $('.status').append("<br>creating -> auth cookie with nonce " + nonce);

            var authCookieURL = apiURL + authstring + "nonce=" + nonce + "&username=" + username + "&password=" + password;

            $.getJSON(authCookieURL, function(data) {
                var items = [];
                $.each(data, function(key, val) {

                    if (key == "cookie") {
                        cookie = val;
                        $('.status').append("<br>Auth cookie -> acquired! value: " + val);

                        // Get a new nonce to create the post:
                        getNonce("posts_auth", "create_post");

                    }
                }); 
            });
        }

        function getid() {

            $('.status').append("<br>Get -> id");

            var authCookieURL = apiURL + authstring + "nonce=" + nonce + "&username=" + username + "&password=" + password;

            $.getJSON(authCookieURL, function(data) {
                var items = [];
                $.each(data, function(key, val) {

                    if (key == "user") {
                        user = val;
                        $('.status').append("<br>id -> acquired! value: " + user.id + "<br>username -> acquired! value: " + user.username + "<br>nicename -> acquired! value: " + user.nicename + "<br>email -> acquired! value: " + user.email + "<br>avatar url -> acquired! value: " + user.avatar);

                        // Get a new nonce to create the post:
                        getNonce("posts_auth", "create_post");  
                    }
                }); 
            });
        }

        function createPost() {
            $('.status').append("<br>creating -> post with nonce: " + nonce);

            var cookiepart = "&cookie=" + cookie;
            var postContent = "&status=publish&title=NonceTest&content=test+test&author=Alex&categories=Demos&tags=test,api,json";

            $.getJSON(apiURL + poststring + "nonce=" + nonce + cookiepart + postContent, function(data) {
                var items = [];
                $.each(data, function(key, val) {
                    if (key == "status") {
                        console.log("status value: " + val);
                        if (val == "ok") {
                            $('.status').append("<br><span class='success'> -> A new post was successfully created.</span>");
                        }
                    }
                });


            });
        }
    }); 
});
</script>

</head>

<body>
<div id="wrapper">
<div class="title">Json Test 3</div>
    <form id="loginForm" method="get" accept-charset="utf-8" name="logme"> 
        <fieldset>
        <div data-role="fieldcontain"> 
            <label for="email"> Username </label> 
            <input type="text" name="username" id="email" value=""> 
        </div> 
        <div data-role="fieldcontain"> 
            <label for="password"> Password </label> 
            <input type="password" name="password" id="password" value=""> 
        </div>
        <input type="button" data-theme="g" name="submit" id="logme" value=" Login "> 
        </fieldset> 
    </form>
<div class="status">Getting nonce for auth cookie...</div>
</div>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

将jquery加载为“http://”。当我尝试使用Phonegap(或Ionicframework)加载远程文件时,我也遇到了这个问题,只是'//'似乎不起作用。

答案 1 :(得分:0)

你的jquery 100%工作。问题是您的apiURL不是来自外部服务器,而是来自localhost。尝试在外部服务器上测试。不要使用localhost