0

私のajax呼び出しは以下のとおりです

<script type="text/javascript">
    function calling() {
        var user = $('#username').val();
        $.ajax({
            type: 'POST',
            //url: "myfirstwebservice.asmx/GetCustomer",
             url: "http://www.mydomain.com/myfirstwebservice.asmx?op=GetCustomer",
            data: "{'username':'" + user + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "jsonp",
            success: function (msg, status) {
                alert("successful");
                console.log(msg.d.password);
                $('#myplace').html("your passowrd is " + msg.d.password);
            },
            error: function (msg, status) {
                alert("failure");
                console.log("failure");
                console.log(msg);
                $('#myplace').html("The status is " + status + " with msg= " + msg + " .<br />" + msg.statusText);
            }
        });
    }
</script>

これは、phonegapで利用可能なオンラインジッパーを介してパッケージ化しています。私のconfig.xmlファイルは

<?xml version="1.0" encoding="UTF-8" ?>
<widget xmlns = "http://www.w3.org/ns/widgets"
xmlns:gap = "http://phonegap.com/ns/1.0"
id        = "com.phonegap.example"
versionCode="10"
version   = "1.0.0">
<name>PhoneGap Example</name>
<description>
    An example for phonegap build docs.
</description>

<author href="https://build.phonegap.com" email="support@phonegap.com">
    arrest warrant
</author>
<access origin="http://www.mydomain.com/" />

その他の詳細は

バージョン1.0.0

PhoneGap 2.1.0

出力が得られないので、間違っていることはありますか?データ型はjsonまたはjsonpである必要がありますか?私のローカルマシンでは、json(データベースアクセス用のクロスドメインであり、正常に動作しています)を使用しています。モバイルデバイスに欠けているもの。Webサービスは.NET2.0フレームワークに含まれています。

4

2 に答える 2

0

jsonpを取得するために使用していたURLは、実際にはjson応答を返していません。

ここをクリックし てください誰かが試してみたい場合、このURLはjsonを返します。

于 2012-11-02T06:06:49.903 に答える
0

私はこれを行うためにWebサービスを使用しません。私は簡略化されたWebフォームを使用し、ファイルの背後にあるコードを使用して、実行したいことを実現します。

これは私のHandler.aspxファイルの内容です:

<% Page Language="C#" AutoEventWireup="True" CodeFile="Handler.aspx.cs" Inherits="Customer_Handler" %>

ファイルの内容は若干異なります。私がしたのは、新しいWebフォームを追加し、aspxファイルの最初の行の後のすべてのHTMLタグを削除することだけでした。Handler.aspx.csファイル内では、とにかくすべてのうなり声の作業を実行します。このファイルとそのHandler.aspxファイルは、Webサイトのアクセス可能な場所に配置する必要があります。

以下は、Handler.aspx.csファイル内に貼り付けることができるコードスニペットです。

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;
        }
    }
}

ajaxタイプに基づいて、上記のswitchステートメントでPOSTまたはGETメソッドを処理できます。次に、変数「op」に基づいて、GETおよびPOST関数ハンドラーで次のswitchステートメントを使用して機能をさらに拡張できます。

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);
}

Get_Customer関数では、渡されたユーザー名に基づいて、実行する必要のあるルックアップを実行する必要があります。必ず有効なjson文字列値を返してください。これは、json文字列のデバッグに役立つ非常に優れたjsonパーサーです。オンラインJSONパーサー これはWebサイトに配置されることを忘れないでください。

private String doGET_Customer(String iUsername){
    String R = "";
    String secret = "";
    // do lookup and set the variable secret ...
    R += "{\"status\":\"OK\", \"password\":\""+ secret +"\"}";
    return R;
}

そして今、Phonegapプロジェクト内のjavascriptからajax関数を呼び出すと、これらの線に沿って何かを使用して、Webサーバー上のサービス(WebForm)と通信できます...

<script type="text/javascript">
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);
        }
    });
}
</script>

私はこれのほとんどをメモリから行ったので、いくつかの構文エラーがあるかもしれません。私はいくつかのプロジェクトでこの手法を使用してきましたが、私のニーズにはうまく機能しました。したがって、上記の構文で問題が発生した場合は、解決できるように最善を尽くします。

また-参考までに:暗号化されていないユーザー情報を安全でないhttpプロトコルを介して送信することはお勧めしません。

お役に立てれば...

于 2012-10-31T16:30:46.447 に答える