Windows Phone 7アクセスAzure Mobile Service(リモートサーバーがエラーを返しました:Notが見つかりません)

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

質問

Azureから新しいモバイルサービスを作成しようとします。 そしてデータはJSONによって正しく公開されています。

https://lifehope.azure-mobile.net/table/userpf

userpfはサンプルテーブルです。

質問を簡素化するために、「みんな」への許可を修正しました。

問題は以下のコードでは機能しないコードです。 エラーメッセージは次のとおりです。リモートサーバーがエラーを返しました:見つかりません userpfで新しいレコードを挿入するには挿入ボタンを押すと...

    private void butInsert_Click(object sender, RoutedEventArgs e)
    {
        USERPF item = new USERPF();
        item.Column1 = 789;
        item.Column2 = 789;

        WebClient wc = new WebClient();
        wc.Headers["Content-Type"] = "application/x-www-form-urlencoded";
        //wc.Headers["X-ZUMO-APPLICATION"] = "";
        wc.UploadStringCompleted += (ss, arg) =>
        {
            if (arg.Error == null)
            {
                MessageBox.Show("OK");
            }
            else
            {
                MessageBox.Show(arg.Error.Message);
            }
        };

        wc.UploadStringAsync(
            new Uri("https://lifehope.azure-mobile.net/tables/USERPF/"),
            "POST", JsonHelper.ObjectToJson(item, typeof(USERPF)));
    }
.

// userpf.cs

public class USERPF
{
    [System.Runtime.Serialization.IgnoreDataMember()]
    public int id { get; set; }
    [System.Runtime.Serialization.DataMember()]
    public int Column1 { get; set; }
    [System.Runtime.Serialization.DataMember()]
    public int Column2 { get; set; }
}
.

// jsonhelper.cs

    public static string ObjectToJson(object obj, Type type)
    {
        try
        {
            //Create a stream to serialize the object to.
            MemoryStream ms = new MemoryStream();

            // Serializer the User object to the stream.
            DataContractJsonSerializer ser = new DataContractJsonSerializer(type);
            ser.WriteObject(ms, obj);
            byte[] json = ms.ToArray();
            ms.Close();
            return Encoding.UTF8.GetString(json, 0, json.Length);
        }
        catch (Exception ex)
        {
           MessageBox.Show(ex.Message);
            return string.Empty;
        }
    }
.

役に立ちましたか?

解決

JSONデータを送信していますが、それは別のコンテンツタイプのものであると言っています。

wc.Headers["Content-Type"] = "application/x-www-form-urlencoded"; 
.

要求に正しいコンテンツタイプを設定します。

wc.Headers["Content-Type"] = "application/json"; 
.

無関係なもの:あなたのタイプが[DataContract]で装飾されていない場合は、Properties Column1とColumn2を[DataMember]で飾る必要はありません。

他のヒント

arg.ErrorをWebExceptionにキャストして、統計コードを確認してください。それは401(不正)

かもしれません
 var webException = arg.Error as WebException;
 if(webException == null) return;


   if (webException.Response != null)
   { 
     var response = (HttpWebResponse)webException.Response; 
     var status  = response.StatusCode; //press F9 here
   }
.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top