Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Post request in web view windows phone

$
0
0

I am trying to achieve the same thing, but I am getting exception when I call the InvokeScript method, Please help. I used this link (http://stackoverflow.com/questions/20646608/post-data-with-a-request-in-a-windows-store-app-webview-using-c-sharp) to code, Here is my JV

         webview.NavigateToString(@"<html>
        <head>
            <script type='text/javascript'>
                function doSomething(userIdValue, sessionIdValue) 
                { 
                    document.getElementById('Username').value = userIdValue;
                    document.getElementById('Password').value = sessionIdValue;
                    document.getElementById('myForm').submit();
                    return 'Hello World!'; 
                }
            </script>
        </head>
        <body>
            <form id='myForm' action='http://integr-dev-ma.test.plano/myplano/Account/Login' method='post'>
                <input type='hidden' id='ext-element-18' name='Username' />
                <input type='hidden' id='ext-element-24' name='Password' />
            </form>
        </body>
    </html>");

When I call this script I get following Exception

     private async void Button_Click_1(object sender, RoutedEventArgs e)
            {
                // this.Frame.Navigate(typeof(SettingsPage));
                try
                {

                    await webview.InvokeScriptAsync("doSomething", new string[] { "30001", "12345" });


                }
                catch (Exception aex)
                {
                    string messages = "";

                    messages += aex.Message + "\r\n";
                    System.Diagnostics.Debug.WriteLine(messages);

                }

enter image description here

I resolved above problem, there was problem in Javascript, The ID was written wrong in following code

    document.getElementById('Username').value = userIdValue;
     document.getElementById('Password').value = sessionIdValue;

The Username and Password should have been "ext-element-18" and "ext-element-18". Correcting javascript removed the exception. Now the basic aim of this request was to login in the webview.

My other team has developed the webpart, they have used sanchatouch framework so in the HTML code of login page there are no

<

form> tags so submitting this didn't worked. I was not loggned in, I am posting source of the login page, may be someone can help on this. When the user clicks on login button normally loginfunction is called, you can locate in code but anyhow I need to do post request to automate login

    <!DOCTYPE html>
    <html>
    <head>
        <!-- General Meta things -->
        <meta charset="utf-8" />
        <title>Login -  myplano</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">




    <script type="text/javascript">

        var userTextfield = new String;

        Ext.setup({

            onReady: function () {
                // create local user model
                Ext.define('LocalUser', {
                    extend: 'Ext.data.Model',
                    config: {
                        fields: [
                            { name: "id" },
                            { name: "username", type: "string" },
                            { name: "firstname", type: "string" },
                            { name: "lastname", type: "string" },
                            { name: "globalmessage", type: "string" },
                            { name: "employeemessage", type: "string" }
                        ],
                        proxy: {
                            type: 'localstorage',
                            id: 'local-user'
                        }
                    }
                });
                var store = Ext.create('Ext.data.Store', {
                    model: 'LocalUser'
                });
                store.load();
                var storageCount = store.getAllCount();
                if (storageCount > 0) {
                    //console.log(storageCount + " users found");
                    var userObject = store.getAt(storageCount - 1);
                    var user = userObject.get("username");
                    userTextfield = user;
                    //console.log("last logged in user was: " + userTextfield);
                }

                //login
                var login = function () {
                    var label = form.down("[itemId='signInFailedLabel']");
                    var btn = form.down("button");
                    label.hide();
                    btn.setDisabled(true);
                    form.setDisabled(true);
                    btn.setText(plano.myplano.Resources.Get("LogingIn"));

                    //login
                    Ext.Ajax.request({
                        url: plano.Base.ResolveUrl('~/Account/Login'),
                        method: 'POST',
                        params: form.getValues(),
                        success: function (response) {
                            var data = Ext.decode(response.responseText);
                            if (data.error) {
                                label.setHtml(data.error);
                                label.show();

                                form.setValues({
                                    Username: "",
                                    Password: ""
                                });

                                btn.setText(plano.myplano.Resources.Get("LoginDescription"));
                                btn.setDisabled(false);
                                form.setDisabled(false);
                            }
                            else if (data.success) {
                                var firstName = "";
                                var lastName = "";
                                var globalMessage = "";
                                var employeeMessage = "";

                                //if new password needed
                                if (data.newPasswordNeeded)
                                {
                                    location.href = plano.Base.ResolveUrl('~/Account/ResetPassword');
                                };
                                Ext.Ajax.request({
                                    url: plano.Base.ResolveUrl("~/Home/getEmployeeName"),
                                    success: function (result) {


                                        var resultData = Ext.decode(result.responseText);

                                        console.log(resultData);

                                        console.log("Employee Name");
                                        console.log(resultData.data['FirstName'] + " " + resultData.data['LastName']);

                                        firstName = resultData.data['FirstName'];
                                        lastName = resultData.data['LastName'];

                                        //Store Username
                                        var thisuser = form.down("textfield").getValue();


                                        Ext.Ajax.request({
                                            url: plano.Base.ResolveUrl("~/Home/getWelcomeText"),
                                            success: function (result2)
                                            {
                                                var resultData2 = Ext.decode(result2.responseText);

                                                console.log("WelcomeText");
                                                console.log(resultData2['employeeMessage']);

                                                globalMessage = resultData2['globalMessage'];

                                                if (resultData2['employeeMessage'])
                                                {
                                                    employeeMessage = resultData2['employeeMessage'];
                                                }

                                                var store = Ext.create('Ext.data.Store', {
                                                    model: "LocalUser"
                                                });
                                                store.load();
                                                store.getProxy().clear();
                                                store.data.clear();
                                                store.add({
                                                    username: thisuser,
                                                    firstname: firstName,
                                                    lastname: lastName,
                                                    globalmessage: globalMessage,
                                                    employeemessage: employeeMessage
                                                });
                                                store.sync();

                                                location.href = plano.Base.ResolveUrl('~/');

                                            }
                                        });

                                    }
                                });


                            }
                        },
                        error: function (errorResponse) {
                            Ext.Msg.alert('Error', 'Login request failed');
                        }
                    });
                };

                var form = Ext.create('Ext.form.Panel', {
                    fullscreen: true,
                    id: 'loginForm',
                    scrollable: null,
                    baseCls: 'login-form',
                    config: {
                        //scrollable: false
                    },
                    layout: {
                        type: 'vbox',
                        pack: 'center',
                        align: 'center'
                    },
                    items: [
                    {
                        xtype: 'panel',
                        hideAnimation: 'fadeOut',
                        showAnimation: 'fadeIn',
                        itemId: 'myplanoLogo',
                        html: '<img id="logo" class="login-myplano-logo" src="/myplano/Content/Images/Vector/myplano.svg" />' //~/Content/Images/myplano 256x256.png
                    }, {
                        xtype: 'fieldset',
                        baseCls: 'login-form',
                        //title: plano.myplano.Resources.Get("LoginTitle"),
                        defaults: {
                            margin: '5px 0',
                            listeners: {
                                action: login
                            }
                        },
                        items: [
                                {
                                    xtype: 'panel',
                                    html: '<div class="separator-login"></div>'
                                },
                                {
                                    xtype: "textfield",
                                    clearIcon: false,
                                    baseCls: 'login-form',
                                    placeHolder: plano.myplano.Resources.Get("UserName"),
                                    value: userTextfield,
                                    name: "Username",
                                    required: true
                                }, {
                                    xtype: "passwordfield",
                                    clearIcon: false,
                                    baseCls: 'login-form',
                                    placeHolder: plano.myplano.Resources.Get("Password"),
                                    name: "Password",
                                    required: true
                                }, {
                                    xtype: 'button',
                                    cls: 'login-button',
                                    labelCls: 'x-button-label login-button-label',
                                    text: plano.myplano.Resources.Get("LoginDescription"),
                                    margin: '5px auto',
                                    handler: login
                                }]
                        }, {
                            xtype: 'label',
                            itemId: 'signInFailedLabel',
                            hidden: true,
                            //hideAnimation: 'fadeOut',
                            //showAnimation: 'fadeIn',
                            style: 'color: #FF8888; text-align: center;',
                            margin: "6 12 6 12"
                        }, {
                            xtype: 'label',
                            itemId: 'vernsionInfo',
                            docked: 'bottom',
                            style: 'text-align: center; color:#FFF; font-size: 0.5em;',
                            margin: "0 0 5px 0",
                            html:
                                'myplano' +
                                ' Version 1' + '.' +
                            '2' +
                            ' Rev. 73666'
                        }, {
                        xtype: 'panel',
                        docked: 'bottom',
                        itemId: 'planoLogo',
                        //hideAnimation: 'fadeOut',
                        //showAnimation: 'fadeIn',
                        //margin: '5% 0 0 0',
                        html: '<img id="logo" class="login-plano-logo" src="/myplano/Content/Images/Vector/plano_logo_white.svg" />' //~/Content/Images/myplano 256x256.png
                    }]
                });

                //hide image if soft keyboard is shown
                var myplanoLogo = form.down("[itemId='myplanoLogo']");
                var planoLogo = form.down("[itemId='planoLogo']");
                window.onresize = function () {
                    // If the current active element is a text input, we can assume the soft keyboard is visible.
                    var windowWidth = Ext.getBody().getWidth(true);
                    var windowHeight = Ext.getBody().getHeight(true);

                    if (windowWidth < windowHeight)
                    {
                        //keyboard is shown
                        console.log("keyboard on");
                        myplanoLogo.show();
                        planoLogo.show();
                    } else {
                        //keyboard is hidden
                        console.log("keyboard off");
                        myplanoLogo.hide();
                        planoLogo.hide();
                    }
                }
            }
        });
    </script>
            </div>

            <script src="/myplano/Scripts/jquery-2.1.1.js"></script>


        </body>
    </html>
    <script>
        //back button disabled
        document.addEventListener("backbutton", Ext.bind(onBackKeyDown, this), false);



        }

    </script>

Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>