我需要使用Ajax和jQuery从HTML5编码页面发送表单。我不知道如何正确地将表单中的参数发送到Ajax和jQuery。如何正确发送? 我的应用程序必须获取用户的推文(来自tweet.com),user_id和tweets amound是参数,必须将其输入到表单中。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>(This is a title) Page example using HTML5</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<style type="text/css">
header {
border-style: dashed;
border-width: 0 0 1px 0;
margin-bottom: 20px;
padding: 10px
}
header h2 {
text-align: center;
}
#tweets article {
margin-bottom: 20px;
}
#tweets article h1 img {
float: left;
}
#tweets article h1 span {
font-size: 14px;
color: blue;
}
#tweets article details span {
font-size: 12px;
color: gray;
}
</style>
</head>
<body>
<header>
<h2>Page header</h2>
</header>
<!-- Control unit -->
<nav></nav>
<!-- Primary unit -->
<section id="content">
<!-- Control units -->
<section id="controls"></section>
<form id="tweetshow" method="post" action="" >
<p>
Enter user_id: <input type="text" name="user_id" />
</p>
<p>
Enter user last tweets amount: <input type="text" name="last_tweets_amount" />
</p>
<p>
From date: <input type="text" name="from_date" />
</p>
<p>
To date: <input type="text" name="to_date" />
</p>
<p>
<input type="button" value="Submit" onClick="AjaxFormRequest()"/>
</p>
</form>
<!-- tweet units -->
<section id="tweets"></section>
</section>
<footer>
<p>Page bottom</p>
</footer>
<script type="text/javascript">
//
// Example AJAX-request executing to Twitter API
//
var params = {
include_entities: true,
include_rts: true,
screen_name: "some_user_id",
count: 3
};
function AjaxFormRequest(user_id, tweets_amount)
{
params.screen_name = user_id;
params.count = tweets_amount;
}
function ShowTweets()
{
$(document).ready(function() {
//Proxy entry point
var apiEndPoint = "http://localhost:8888/1.1/";
// Request parameters are here
// https://dev.twitter.com/docs/api/1/get/statuses/user_timeline
// To execute async-request using jQuery
$.ajax({
// Full URL to source
url: apiEndPoint + "statuses/user_timeline.json",
// HTTP-request type
type: "POST",
// Returned data type
dataType: "jsonp",
// Parameters
data: params,
// method if response has been successful
success: function (response) {
// look all response array
$.each(response, function (i, item) {
//console.dir(item);
// to wrap data into html and insert into #tweets section
var $header = $("<h1>")
.append($("<img>", { src: item.user.profile_image_url}))
.append($("<span>").html(item.user.name));
var $text = $("<p>").html(item.text);
var $details = $("<details>").append(
$("<span>").html(item.created_at)
);
$("<article>", { id: item.id })
.append($header, $text, $details)
.appendTo("#tweets");
});
},
// method if response has not been successful
error: function (xhr, status, error) {
alert("An error is occured: " + status);
}
});
/*
OR
we can use short method
$.getJSON("https://api.twitter.com/1/statuses/user_timeline.json?callback=?", params, function(data) {
console.dir(data);
})
.error(function(xhr, status, error) {
console.log(xhr);
});
*/
});
}
</script>
</body>
</html>
答案 0 :(得分:0)
将您的ajax呼叫数据设置为
data : $("#tweetshow").serialize();
Serialize()将表单字段作为URL编码表示法中的字符串返回。
答案 1 :(得分:0)
你可以做Barry所说的,也可以使用
data: JSON.stringify(params)
在你的Ajax调用中。