我正在使用插件劫持表单并通过插件中的 ajax 函数发布它。我可以发布表格并在控制台中获得结果,但我无法让表格发布并正常工作。
我当前的构建正在使用:
Laravel - PHP 框架 Instagram PHP 类 - 使用 php Ajax 插件与 instagram api 交互 - 在不刷新页面的情况下发布表单。
目前我已经为 ajax 代码(插件)得到了这个:
在我的functions.js文件中,我有这个代码来发布表单:
$(document).ready(function() {
$('.forms').each(function() {
$(this).formSubmit({
// Shows the formData that will be submitted; return false
// to prevent the form from being submitted
before: function(formData) {
if( console ) {
console.log('`before` callback:');
console.log(formData);
}
},
// Shows the AJAX response when the form is accepted
success: function(data) {
if( console ) {
console.log('`success` callback:');
console.log(data);
}
},
// Shows the AJAX response when the form is rejected
error: function(data) {
if( console ) {
console.log('`error` callback:');
console.log(data);
}
},
// Shows the AJAX error if one occurs
ajaxError: function(textStatus, errorThrown) {
if( console ) {
console.log('`ajaxError` callback:');
console.log(textStatus);
console.log(errorThrown);
}
},
// These add and remove a custom class called 'whoops' to the
// parent element of all invalid fields
showInvalid: function() {
$(this).parent().addClass('whoops');
},
hideInvalid: function() {
$(this).parent().removeClass('whoops');
}
});
});
});
下面是环绕图像的表单的标记,因此您可以通过表单提交中的 instagram api 发送喜欢/不喜欢。
try {
$instagram = new Instagram\Instagram;
$instagram->setAccessToken($_SESSION['instagram_access_token']);
$token = $_SESSION['instagram_access_token'];
//$clientID = $_SESSION['client_id'];
$current_user = $instagram->getCurrentUser();
$tag = $instagram->getTag('folkclothing');
$media = $tag->getMedia(isset($_GET['max_tag_id']) ? array( 'max_tag_id' => $_GET['max_tag_id'] ) : null);
$liked_media = $current_user->getLikedMedia();
/* echo 'https://api.instagram.com/v1/media/'. $item->getId() .'/likes?access_token='.$token.''; */
if ( isset( $_POST['action'] ) ) {
echo '<br/>FORM IS SUBMITTED, INSPECT WHAT WAS SENT';
print_r($_POST);
$id = $_POST['id'];
switch( strtolower( $_POST['action'] ) ) {
case 'like':
$current_user->addLike( $id );
break;
case 'unlike':
$current_user->deleteLike( $id );
break;
}
}
} catch ( Exception $e ) {
// yes there is an error
$error = $e->getMessage();
}
echo '<section id="images">';
foreach ( $media as $item ) {
echo '<article class="instagram-image">';
echo '<form class="forms" action="'; echo URL::current(); echo '" method="post">';
$id = $item->getId();
echo '<a title="' . $item->getCaption() .'" class="fancybox" href="' . $item->link . '"><img alt="' . $item->getCaption() .'" src="' . $item->images->standard_resolution->url . '" /></a>';
echo '<div class="formSubmit-feedback"></div>';
echo '<img src="/public/img/377.gif" alt="loader"/>';
if ( $current_user->likes($item) ){
echo '<button class="ajax instabtn unlike icon-heart" type="submit" name="action" value="Unlike"></button>';
} else {
echo '<button class="ajax instabtn like icon-heart" type="submit" name="action" value="Like"></button>';
}
echo '<input type="hidden" name="id" value="'; echo $id; echo '">';
echo '<p>'; echo $item->likes->count; echo '</p>';
echo '</form>';
echo '</article>';
}
echo '</section>';
我在发布表单时遇到的错误如下,它找到了要发布的对象的正确 ID,但它不会发布到服务器:
`before` callback: functions.js:51
Object {id: "509073517011482478_5670460"}
我知道它很近,但我不确定在哪里看它。