【问题标题】:AJAX vs HTTPRequestAJAX 与 HTTPRequest
【发布时间】:2013-07-19 11:38:30
【问题描述】:

我正在将 WebAppication 重写为 Windows Phone 上的本机应用程序。

我遇到了一点麻烦,无法发布数据。

这是 WebApplication 的代码 - 它可以工作:

$.ajax({
        url: 'https://blabla.blabla.com/mobile-api/v1/security',
        contentType: 'application/json',
        // data: JSON.stringify ({"jsonrpc": "2.0", "method": "login", "params":["TestLogin", "SuperPass"], "id": 1}),
        data: JSON.stringify({"jsonrpc": "2.0", "method": "login", "params":[username, password], "id": 1}),  
        type:"POST",
        success: function(data){
            callback.onSuccess(data);
        },
        error: function(xhr){
            callback.onError(xhr.status);
        }
    });

这是我的 C# 代码:

private void PostLoginData()
    {
        HttpWebRequest request = HttpWebRequest.CreateHttp("https://blabla.blabla.com/mobile-api/v1/security");
        request.Method="POST";
        request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback),request);
    }
    void GetRequestStreamCallback(IAsyncResult callbackResult)
    {
        HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
        // End the stream request operation
        Stream postStream = myRequest.EndGetRequestStream(callbackResult);

        // Create the post data
        string postData = "{\"jsonrpc\": \"2.0\", \"method\": \"login\", \"params\":{\"username\":" + "\"TestLogin\"" + ", \"password\":" + "\"SuperPass\"" + "}, \"id\": 1}";
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);

        // Add the post data to the web request
        postStream.Write(byteArray, 0, byteArray.Length);
        postStream.Close();

        // Start the web request
        myRequest.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), myRequest);
    }
    void GetResponsetStreamCallback(IAsyncResult callbackResult)
    {
        HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
        using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
        {
            string result = httpWebStreamReader.ReadToEnd();
            //For debug: show results
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                MessageBox.Show(result);
            });
        }
    }

响应返回我:

方法参数无效。

如果我尝试发布不带“”的登录名和密码,如下所示:

string postData = "{\"jsonrpc\": \"2.0\", \"method\": \"login\", \"params\":{\"username\":" + "TestLogin" + ", \"password\":" + "SuperPass" + "}, \"id\": 1}";

回复是:

解析错误

登录名和密码 100% 正确...我认为问题出在发布数据中,因为 WebApplications 请求有效

【问题讨论】:

    标签: c# ajax windows-phone-7 windows-phone-8 httprequest


    【解决方案1】:

    正确的字符串应该是:

    string postData = "{\"jsonrpc\": \"2.0\", \"method\": \"login\", \"params\":[" + "\""+Username+"\"" + "," + "\""+UserPassword+"\"" + "], \"id\": 1}";
    

    【讨论】:

      猜你喜欢
      • 2012-01-29
      • 1970-01-01
      • 2016-08-25
      • 1970-01-01
      • 2012-09-04
      • 2013-10-07
      • 1970-01-01
      • 1970-01-01
      • 2016-08-25
      相关资源
      最近更新 更多