#include
#include
#include //定义了MFC CInternetSession类等
bool PostHttpPage(const std::string& hostName, const std::string& pathName, const std::string& postData)
{
using namespace std;
CInternetSession session("your app agent name");
try
{
INTERNET_PORT nPort = 80;
DWORD dwRet = 0;
CHttpConnection* pServer = session.GetHttpConnection(hostName.c_str(), nPort);
CHttpFile* pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST, pathName.c_str());
CString strHeaders = "Content-Type: application/x-www-form-urlencoded"; // 请求头
//开始发送请求
pFile->SendRequest(strHeaders,(LPVOID)postData.c_str(),postData.size());
pFile->QueryInfoStatusCode(dwRet);
if (dwRet == HTTP_STATUS_OK)
{
CString result, newline;
while(pFile->ReadString(newline))
{//循环读取每行内容
result += newline+"\r\n";
}
std::cout< }
else
{
return false;
}
delete pFile;
delete pServer;
}
catch (CInternetException* pEx)
{
//catch errors from WinInet
TCHAR pszError[200];
pEx->GetErrorMessage(pszError, 200);
std::cout< return false;
}
session.Close();
return true;
}
int main(void)
{
//向http://current.sinaapp.com/post.php发送数据
PostHttpPage("current.sinaapp.com","post.php","name=rain&age=12");
}
2011年11月10日星期四
C++ 以POST方式向网页提交数据
示例代码如下:
2011年8月20日星期六
jQuery.post()函数的使用方法
函数原型: jQuery.post( url, [data], [success(data, textStatus, jqXHR)], [dataType] )
参数说明:
url 请求发送的地址
data 随请求发送的一个关联对象或者一个字符串
success(data, textStatus, jqXHR) 指定请求成功时执行的一个callback函数
dataType 预期从服务器接收的数据类型. 默认: 智能判断 (xml, json, script, or html).
返回值类型:jqXHR
此函数是下面函数的简写形式
使用示例:
关于dataType参数的详细说明
期待从服务器返回的数据类型. 如果不指定, jQuery 将基于返回结果的MIME类型 (XML MIME 类型将产生 XML, in 1.4 JSON 将产生 一个 JavaScript 对象, in 1.4 script 将执行 script, and 其他将作为字符串返回)尝试推断它. 可用的类型 (和传递给 success callback 函数第一个参数最终的结果) 是:
"xml": 返回一个可以通过jQuery解析的 XML 文档 .
"html": 把 HTML 作为普通文本返回; included script tags are evaluated when inserted in the DOM.
"script": 将返回值作为 JavaScript对待并以普通文本方式返回. Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true. Note: This will turn POSTs into GETs for remote-domain requests.
"json": 将返回值作为 JSON 对待 并返回一个 JavaScript 对象. In jQuery 1.4 JSON 数据使用严格的方式解析; 任何格式异常的JSON 都被拒绝并抛出一个parse error. (更多关于正确的JSON格式请参考 json.org 网站.)
"jsonp": Loads in a JSON block using JSONP. Adds an extra "?callback=?" to the end of your URL to specify the callback. Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true.
"text": 普通文本字符串.
多个以空格分隔的值: 从 jQuery 1.5, jQuery 能够将从Content-Type头中得到的dataType转换为你需要的其他类型 . 例如,如果你想将一个文本返回值作为XML对待,使用 "text xml" 作为 dataType. You can also make a JSONP request, have it received as text, and interpreted by jQuery as XML: "jsonp text xml." Similarly, a shorthand string such as "jsonp xml" will first attempt to convert from jsonp to xml, and, failing that, convert from jsonp to text, and then from text to xml.
参数说明:
url 请求发送的地址
data 随请求发送的一个关联对象或者一个字符串
success(data, textStatus, jqXHR) 指定请求成功时执行的一个callback函数
dataType 预期从服务器接收的数据类型. 默认: 智能判断 (xml, json, script, or html).
返回值类型:jqXHR
此函数是下面函数的简写形式
$.ajax({
type: 'POST',
url: url,
data: data,
success: success,
dataType: dataType
});
使用示例:
$.post("test.php", { "func": "getNameAndTime" },
function(data){
console.log(data.name); // John
console.log(data.time); // 2pm
}, "json")
.success(function() { alert("second success"); })
.error(function() { alert("error"); })
.complete(function() { alert("complete"); });
关于dataType参数的详细说明
期待从服务器返回的数据类型. 如果不指定, jQuery 将基于返回结果的MIME类型 (XML MIME 类型将产生 XML, in 1.4 JSON 将产生 一个 JavaScript 对象, in 1.4 script 将执行 script, and 其他将作为字符串返回)尝试推断它. 可用的类型 (和传递给 success callback 函数第一个参数最终的结果) 是:
"xml": 返回一个可以通过jQuery解析的 XML 文档 .
"html": 把 HTML 作为普通文本返回; included script tags are evaluated when inserted in the DOM.
"script": 将返回值作为 JavaScript对待并以普通文本方式返回. Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true. Note: This will turn POSTs into GETs for remote-domain requests.
"json": 将返回值作为 JSON 对待 并返回一个 JavaScript 对象. In jQuery 1.4 JSON 数据使用严格的方式解析; 任何格式异常的JSON 都被拒绝并抛出一个parse error. (更多关于正确的JSON格式请参考 json.org 网站.)
"jsonp": Loads in a JSON block using JSONP. Adds an extra "?callback=?" to the end of your URL to specify the callback. Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true.
"text": 普通文本字符串.
多个以空格分隔的值: 从 jQuery 1.5, jQuery 能够将从Content-Type头中得到的dataType转换为你需要的其他类型 . 例如,如果你想将一个文本返回值作为XML对待,使用 "text xml" 作为 dataType. You can also make a JSONP request, have it received as text, and interpreted by jQuery as XML: "jsonp text xml." Similarly, a shorthand string such as "jsonp xml" will first attempt to convert from jsonp to xml, and, failing that, convert from jsonp to text, and then from text to xml.
订阅:
博文 (Atom)