HttpClient发布数据不适用于javascript?

时间:2013-10-23 17:11:02

标签: javascript windows-8.1

我正在尝试编写程序来登录我的网站。但我发送邮件请求到网页有问题。

这是我的代码

的script.js

function doLogin() {
    httpClient = new Windows.Web.Http.HttpClient();
    httpClient.defaultRequestHeaders.userAgent.parseAdd("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
    var outputField = document.getElementById("test");

    var resourceAddress = new Windows.Foundation.Uri('http://localhost/test.php');
    var stringContent = new Windows.Web.Http.HttpStringContent("Data1=Value1&Data2=value");

    httpPromise = httpClient.postAsync(resourceAddress, stringContent).then(function (response) {
        return Helpers.displayTextResultAsync(response, outputField);
    });
    httpPromise.done(function () {

    }, Helpers.onError);
}

test.php的

<?php
     print_r($_SERVER);
     print_r($_POST);
?>

根据要求回复

200 OK
Connection: Keep-Alive
Server: Apache/2.4.4 (Win32) OpenSSL/1.0.1e PHP/5.5.3
Keep-Alive: timeout=5, max=100
Date: Wed, 23 Oct 2013 17:01:56 GMT
X-Powered-By: PHP/5.5.3
Content-Length: 1812
Content-Type: text/html

Array
(
    [MIBDIRS] => /xampp/php/extras/mibs
    [MYSQL_HOME] => \xampp\mysql\bin
    [OPENSSL_CONF] => /xampp/apache/bin/openssl.cnf
    [PHP_PEAR_SYSCONF_DIR] => \xampp\php
    [PHPRC] => \xampp\php
    [TMP] => \xampp\tmp
    [HTTP_ACCEPT_ENCODING] => gzip, deflate
    [HTTP_USER_AGENT] => Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)
    [CONTENT_LENGTH] => 54
    [CONTENT_TYPE] => text/plain; charset=UTF-8
    [HTTP_HOST] => localhost
    [HTTP_CONNECTION] => Keep-Alive
    [HTTP_CACHE_CONTROL] => no-cache
    [PATH] => C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\
    [SystemRoot] => C:\Windows
    [COMSPEC] => C:\Windows\system32\cmd.exe
    [PATHEXT] => .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
    [WINDIR] => C:\Windows
    [SERVER_SIGNATURE] => <address>Apache/2.4.4 (Win32) OpenSSL/1.0.1e PHP/5.5.3 Server at localhost Port 80</address>

    [SERVER_SOFTWARE] => Apache/2.4.4 (Win32) OpenSSL/1.0.1e PHP/5.5.3
    [SERVER_NAME] => localhost
    [SERVER_ADDR] => ::1
    [SERVER_PORT] => 80
    [REMOTE_ADDR] => ::1
    [DOCUMENT_ROOT] => D:/xampp/htdocs
    [REQUEST_SCHEME] => http
    [CONTEXT_PREFIX] => 
    [CONTEXT_DOCUMENT_ROOT] => D:/xampp/htdocs
    [SERVER_ADMIN] => postmaster@localhost
    [SCRIPT_FILENAME] => D:/xampp/htdocs/test.php
    [REMOTE_PORT] => 2092
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_PROTOCOL] => HTTP/1.1
    [REQUEST_METHOD] => POST
    [QUERY_STRING] => 
    [REQUEST_URI] => /test.php
    [SCRIPT_NAME] => /test.php
    [PHP_SELF] => /test.php
    [REQUEST_TIME_FLOAT] => 1382547716.784
    [REQUEST_TIME] => 1382547716
)
Array
(
)

我在http://code.msdn.microsoft.com/windowsapps/HttpClient-sample-55700664尝试了Microsoft示例代码参考,但它似乎无效。

1 个答案:

答案 0 :(得分:2)

您必须将Content-Type设置为application/x-www-form-urlencoded。即:

var stringContent = new Windows.Web.Http.HttpStringContent(
    "Data1=Value1&Data2=value",
    Windows.Storage.Streams.UnicodeEncoding.utf8,
    "application/x-www-form-urlencoded");