Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wouldn't use a web service to do this.. I use a stripped down web form and it's code behind file to achieve what you're wanting to do.</p> <p>This is my Handler.aspx file contents:</p> <pre><code>&lt;% Page Language="C#" AutoEventWireup="True" CodeFile="Handler.aspx.cs" Inherits="Customer_Handler" %&gt; </code></pre> <p>Your file contents will vary slightly. All I did was add a new web form and remove all HTML tags after the first line of the aspx file. Inside the Handler.aspx.cs file is where we perform all the grunt work anyways. This file and its Handler.aspx file should be placed somewhere accessible on your web site.</p> <p>The following are code snippets, that you can paste within your Handler.aspx.cs file.</p> <pre><code>using System; using System.Collections; using System.Configuration; using System.Data; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; public partial class Customer_Handler : System.Web.UI.Page private String pCallback = ""; private String pOP= ""; private String pUserName = ""; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (!String.IsNullOrEmpty(Request.QueryString["callback"])) { pCallback = Request.QueryString["callback"]; } if (!String.IsNullOrEmpty(Request.QueryString["op"])) { pOP = Request.QueryString["op"]; } if (!String.IsNullOrEmpty(Request.QueryString["username"])) { pUserName = Request.QueryString["username"]; } switch (Request.HttpMethod) { case "GET": doGET_Function(); break; case "POST": doPOST_function(); break; default: break; } } } </code></pre> <p>Based on the ajax type we can handle POST or GET Methods with the above switch statement. And then based on the variable "op" we can further extend our functionality using the following switch statements in the GET and POST Function Handlers.</p> <pre><code>private void doGET_Functions() { String R = ""; Response.Clear(); Response.ContentType = "text/plain"; // Each function needs to return a valid JSON String... switch (pOP.ToLower()) { case "getcustomer": R = doGET_Customer(pUserName); break; //etc... default: R = "{\"status\":\"FAILED\", \"statusText\":\"No OP CMD Given...\"}"; break; } if (!String.IsNullOrEmpty(pCallback)) { R = pCallback + "(" + R + ")"; } Response.Write(R); } private void doPOST_functions() { String R = ""; Response.Clear(); Response.ContentType = "text/plain"; // Each function needs to return a vaild JSON String... switch (pOP.ToLower()) { case "addcustomer": // Save New Customer .. //R = doAdd_Customer(); break; //etc... default: R = "{\"status\":\"FAILED\", \"statusText\":\"No OP CMD Given...\"}"; break; } if (!String.IsNullOrEmpty(pCallback)) { R = pCallback + "(" + R + ")"; } Response.Write(R); } </code></pre> <p>In your Get_Customer function you'll need to do whatever lookup you're needing to do based on the username passed. Be sure to return a valid json string value. Here is a very good json parser that you can use to help debug your json strings <a href="http://json.parser.online.fr/" rel="nofollow">Online JSON Parser</a> Remember this is going to be located on your website..</p> <pre><code>private String doGET_Customer(String iUsername){ String R = ""; String secret = ""; // do lookup and set the variable secret ... R += "{\"status\":\"OK\", \"password\":\""+ secret +"\"}"; return R; } </code></pre> <p>And now when we call the ajax function from our javascript within our Phonegap project we can use something along these lines to communicate with the service (WebForm) on the web server...</p> <pre><code>&lt;script type="text/javascript"&gt; function calling() { var user = $('#username').val(); $.ajax({ type: 'GET', url: "http://www.mydomain.com/handler.aspx", data: { op: "GetCustomer", username: user } dataType: "jsonp", timeout: 20000, async: false, success: function (json) { alert(json.status); if (json.status == 'OK'){ console.log(json.password); $('#myplace').html("your passowrd is " + json.password); } else { $('#myplace').html("The status is " + json.status + " with msg= " + json.statusText); } }, error: function (e) { alert("failure: "+e.message); console.log("failure: "+e.message); } }); } &lt;/script&gt; </code></pre> <p>I may have a few syntax errors since I did most of this from memory. I've used this technique in several of my projects and it has worked well for my needs. So, if you have any problems with the above syntax, I'll do my best to help sort it out..</p> <p>Also - Just an FYI: I wouldn't recommend sending any unencrypted user information over an unsecure http protocol.</p> <p>Hope this Helps...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload