dtbiszu7724
2015-11-08 04:39
浏览 40

使用Parse.com Javascript API与通过PHP API进行身份验证的用户

If my site uses the Parse.com PHP API to authenticate users, can I use the Javascript API for ajax requests in such a way that the user, having been logged in via PHP, can access data via the Javascript API?

What would I need to set up differently?


Some background

I have a site that uses the Parse.com PHP API to handle all backend tasks like storing and retrieving data and authenticating users.

I have several pages that use ajax to refresh page content.

I have written my own Javascript class for sending JSON data to my server via ajax where the info is processed and my PHP code steps in to create, update, or delete Parse objects as needed.

This all works very well with the exception of dealing with files. I see several approaches to dealing with files but I feel like I'm re-inventing the wheel.

The Parse.com Javascript API is great, I'm pretty familiar with it and it already deals with files really well. It would be awesome If I could use it for the ajax.


I have tried...

Logging in with the PHP API and then attempting to use the Javascript SDK directly to get the current user like

    var currentUser = Parse.User.current();
    if (currentUser) {
        // do stuff with the user
        alert(currentUser);
    } else {
        // show the signup or login page
        alert('that didnt work');
    }

This did not work.

图片转代码服务由CSDN问答提供 功能建议

如果我的网站使用Parse.com PHP API对用户进行身份验证,我是否可以将Javascript API用于ajax请求 用户通过PHP登录后可以通过Javascript API访问数据吗?

我需要设置不同的设置吗?


某些背景

我的网站使用Parse.com PHP API来处理所有后端任务,例如存储和检索数据以及验证用户。< / p>

我有几个页面使用ajax刷新页面内容。

我编写了自己的Javascript类,用于通过ajax将JSON数据发送到我的服务器 信息被处理,我的PHP代码根据需要介入创建,更新或删除Parse对象。

除了处理文件外,这一切都很有效。 我看到了几种处理文件的方法,但我觉得我正在重新发明轮子。

Parse.com Javascript API很棒,我对它很熟悉 已经处理好文件了。 如果我可以将它用于ajax那就太棒了。


我试过...

登录 使用PHP API,然后尝试直接使用Javascript SDK来获取当前用户

  var currentUser = Parse.User.current(); 
 if(currentUser)  {
 //使用用户进行操作
 alert(currentUser); 
}其他{
 //显示注册或登录页面
警报('没有工作'); 
} 
   
 
 

这不起作用。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongxing7530 2015-11-09 06:43
    已采纳

    It turns out that you can do this by passing the Parse PHP session token to the Javascript API like this:

       <?php
            $sessionToken = ParseUser::getCurrentUser()->getSessionToken();
            echo 'var token="'.$sessionToken.'";';
        ?>
    
    
        Parse.User.become(token).then(function (user) {
            // The current user is now set to user.
            alert('User has access to javascript API');
        }, function (error) {
            // The token could not be validated.
            alert('Error: ' + error.code + ' ' + error.message);
        });
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题