How to make run a javascript function that calls a web service from another project

StackOverflow https://stackoverflow.com/questions/22622680

  •  20-06-2023
  •  | 
  •  

سؤال

In my Phonegap project I need to call a web service that does some login stuff. I can run the web service and make it visible; but when I try to call it from a login button's on-click; it doesn't return any errors nor do anything. Thanks for help.

that is userLogin.js

function login() {
$("#loginButon").click(function() {
                    var requestData = {
                        email : $('#email').val(),
                        password : $('#password').val()
                    };

                    $.ajax({
                                type : 'POST',
                                dataType : 'json',
                                headers: { 'Access-Control-Allow-Origin': '*' },
                                origin: 'http://localhost:8080',
                                contentType : 'application/json; charset=utf-8',
                                url : 'http://localhost:8080/myWebServiceProjectName/ws/userLogin/userLoginControl',
                                data : JSON.stringify(requestData),
                                crossDomain: true,
                                success : function(responseData) {
                                    onSuccess(responseData);
                                }
                            });
                });

}

This is my login.html

<html>
<body>
<head>
<script src="js/jqm.globals.js"></script>
<script src="jsAndroid/jquery.min.js"></script>
<script src="jsAndroid/userLogin.js"></script>
<script src="http://code.jquery.com/mobile/latest/jquery.mobile.min.js"></script>
</head>
<body>
<div data-role="page">

    <div data-role="header" style="background: #31B404; color: white;">
        <h1>WELCOME</h1>
    </div>

    <div data-role="content">
        <div id="ortala">
            <div class="span-2 fl">
                <!--login-->
                <div class="controls-login">
                    <div class="form-login">
                        <h3>GIRIS</h3>
                        <form>
                            <div class="form-item">
                                <input type="text" id="email" placeholder="Kullanici Adi" />
                            </div>
                            <div class="form-item">
                                <input type="password" id="password" />
                            </div>

                            <div class="form-item">
                                <a href="yeniUyeEkleme.html" rel="external">Uye Degil
                                    misiniz?</a>
                            </div>
                            <div class="form-item">
                                <input type="button" id="loginButon" value="Giriş Yap"
                                    onclick="login();"
                                    style="background-color: FFFFFF; color: #090101;" />
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

هل كانت مفيدة؟

المحلول 2

I figured it out on my own. It was all about chrome itself.

I run chrome from command window with these parameters:

start chrome.exe allow-file-access-from-files --disable-web-security

Thanks again for helps.

نصائح أخرى

You are calling a function to setup an event listener.

Remove the onclick="login();" from the input button and replace login() with the following:

$(function() {
    $("#loginButon").click(function() {
        var requestData = {
            email : $('#email').val(),
            password : $('#password').val()
        };

        var request = $.ajax({
            type : 'POST',
            dataType : 'json',
            contentType : 'application/json; charset=utf-8',
            url : 'http://localhost:8080/myWebServiceProjectName/ws/userLogin/userLoginControl',
            data : JSON.stringify(requestData),
            crossDomain: true
        });

        request.done(function(data) {
            console.log(data);
            onSuccess(responseData);
        });

        request.fail(function(jqXHR, textStatus) {
            console.log("request error: " + textStatus);
        });
    });
});

Make sure to remove the console.log() frpm both done & fail events if you are using IE, or when you are done testing as it will break some browsers.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top