for (var i in tokens) {
var id = tokens[i].substring(0, tokens[i].indexOf('='));
var name = tokens[i].substring(tokens[i].indexOf('=') + 1);
.
.
.
var button = document.createElement('button');
button.id = id;
$(button).addClass('friend-invite-button')
.html('Invite')
.click(function() {
$.ajax({ url: path + '/twitter/directmessage.php',
data: {'screen_name': name},
type: 'post',
success: function(output) { alert(output); }
});
});
}
上面的代码似乎将相同的变量“名称”值附加到点击事件。每次单击按钮时,我都会在警报中得到相同的值,并且该值是我通过循环获得的最后一个值。你能纠正我吗?我的php文件在下面
<?php
session_start();
require_once('twitteroauth.php');
require_once('config.php');
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
$request_token = $connection->getRequestToken(OAUTH_CALLBACK);
$_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
switch ($connection->http_code) {
case 200:
$options = array("screen_name" => $_POST["screen_name"], "text" => "message");
$status = $connection->post('direct_messages/new', $options);
echo $_POST["screen_name"];
break;
default:
echo 'Could not connect to Twitter. Refresh the page or try again later.';
}