2011-08-27 4 views
1

WordPress 플러그인에서 JavaScript로 로그인, 등록 Facebook API를 사용하고 있습니다. 이 오류가 발생했습니다JavaScript에서 json.stringify가 함수 오류가 아닙니다.

json.stringify is not a function error at JavaScript. 

무엇이 문제입니까? 그런 다음 오류가 멀리 가야 코드에 있음을 추가하는 경우

http://www.json.org/js.html

-

<div id="fb-root"></div> 
<script type="text/javascript"> 
    var button; 
    var userInfo; 

    window.fbAsyncInit = function() { 
     FB.init({ appId: '154333274632806', 
      status: true, 
      cookie: true, 
      xfbml: true, 
      oauth: true}); 

     showLoader(true); 

     function updateButton(response) { 
      button  = document.getElementById('fb-auth'); 
      userInfo  = document.getElementById('user-info'); 

      if (response.authResponse) { 
       //user is already logged in and connected 
       FB.api('/me', function(info) { 
        login(response, info); 
       }); 

       button.onclick = function() { 
        FB.logout(function(response) { 
         logout(response); 
        }); 
       }; 
      } 
      else { 
       //user is not connected to your app or logged out 
       button.innerHTML = 'Login'; 
       button.onclick = function() { 
        showLoader(true); 
        FB.login(function(response) { 
         if (response.authResponse) { 
          FB.api('/me', function(info) { 
           login(response, info); 
          }); 
         } else { 
          //user cancelled login or did not grant authorization 
          showLoader(false); 
         } 
        }, {scope:'email,user_birthday,status_update,publish_stream,user_about_me'}); 
       } 
      } 
     } 

     // Run once with current status and whenever the status changes. 
     FB.getLoginStatus(updateButton); 
     FB.Event.subscribe('auth.statusChange', updateButton); 
    }; 
    (function() { 
     var e = document.createElement('script'); e.async = true; 
     e.src = document.location.protocol 
      + '//connect.facebook.net/en_US/all.js'; 
     document.getElementById('fb-root').appendChild(e); 
    }()); 

    function login(response, info){ 
     if (response.authResponse) { 
      var accessToken        = response.authResponse.accessToken; 

      userInfo.innerHTML        = '<img src="https://graph.facebook.com/' + info.id + '/picture">' + info.name 
                  + "<br /> Your Access Token: " + accessToken; 
      button.innerHTML        = 'Logout'; 
      showLoader(false); 
      document.getElementById('other').style.display = "block"; 
     } 
    } 

    function logout(response){ 
     userInfo.innerHTML        = ""; 
     document.getElementById('debug').innerHTML  = ""; 
     document.getElementById('other').style.display = "none"; 
     showLoader(false); 
    } 

    //stream publish method 
    function streamPublish(name, description, hrefTitle, hrefLink, userPrompt){ 
     showLoader(true); 
     FB.ui(
     { 
      method: 'stream.publish', 
      message: '', 
      attachment: { 
       name: name, 
       caption: '', 
       description: (description), 
       href: hrefLink 
      }, 
      action_links: [ 
       { text: hrefTitle, href: hrefLink } 
      ], 
      user_prompt_message: userPrompt 
     }, 
     function(response) { 
      showLoader(false); 
     }); 
    } 

    function showStream(){ 
     FB.api('/me', function(response) { 
      //console.log(response.id); 
      streamPublish(response.name, 'I like the articles of Thinkdiff.net', 'hrefTitle', 'http://thinkdiff.net', "Share thinkdiff.net"); 
     }); 
    } 

    function share(){ 
     showLoader(true); 
     var share = { 
      method: 'stream.share', 
      u: 'http://thinkdiff.net/' 
     }; 

     FB.ui(share, function(response) { 
      showLoader(false); 
      console.log(response); 
     }); 
    } 

    function graphStreamPublish(){ 
     showLoader(true); 

     FB.api('/me/feed', 'post', 
      { 
       message  : "I love thinkdiff.net for facebook app development tutorials", 
       link  : 'http://ithinkdiff.net', 
       picture  : 'http://thinkdiff.net/iphone/lucky7_ios.jpg', 
       name  : 'iOS Apps & Games', 
       description : 'Checkout iOS apps and games from iThinkdiff.net. I found some of them are just awesome!' 
     }, 
     function(response) { 
      showLoader(false); 

      if (!response || response.error) { 
       alert('Error occured'); 
      } else { 
       alert('Post ID: ' + response.id); 
      } 
     }); 
    } 

    function fqlQuery(){ 
     showLoader(true); 

     FB.api('/me', function(response) { 
      showLoader(false); 

      //http://developers.facebook.com/docs/reference/fql/user/ 
      var query  = FB.Data.query('select name, profile_url, sex, pic_small from user where uid={0}', response.id); 
      query.wait(function(rows) { 
       document.getElementById('debug').innerHTML = 
       'FQL Information: '+ "<br />" + 
       'Your name: '  + rows[0].name               + "<br />" + 
       'Your Sex: '  + (rows[0].sex!= undefined ? rows[0].sex : "")       + "<br />" + 
       'Your Profile: ' + "<a href='" + rows[0].profile_url + "'>" + rows[0].profile_url + "</a>" + "<br />" + 
       '<img src="'  + rows[0].pic_small + '" alt="" />' + "<br />"; 
      }); 
     }); 
    } 

    function setStatus(){ 
     showLoader(true); 

     status1 = document.getElementById('status').value; 
     FB.api(
      { 
      method: 'status.set', 
      status: status1 
      }, 
      function(response) { 
      if (response == 0){ 
       alert('Your facebook status not updated. Give Status Update Permission.'); 
      } 
      else{ 
       alert('Your facebook status updated'); 
      } 
      showLoader(false); 
      } 
     ); 
    } 

    function showLoader(status){ 
     if (status) 
      document.getElementById('loader').style.display = 'block'; 
     else 
      document.getElementById('loader').style.display = 'none'; 
    } 

</script> 

<h3>New JavaScript SDK & OAuth 2.0 based FBConnect Tutorial | Thinkdiff.net</h3> 
<button id="fb-auth">Login</button> 
<div id="loader" style="display:none"> 
    <img src="ajax-loader.gif" alt="loading" /> 
</div> 

<br /> 
<div id="user-info"></div> 
<br /> 
<div id="debug"></div> 

<div id="other" style="display:none"> 
    <a href="#" onClick="showStream(); return false;">Publish Wall Post</a> | 
    <a href="#" onClick="share(); return false;">Share With Your Friends</a> | 
    <a href="#" onClick="graphStreamPublish(); return false;">Publish Stream Using Graph API</a> | 
    <a href="#" onClick="fqlQuery(); return false;">FQL Query Example</a> 

    <br /> 
    <textarea id="status" cols="50" rows="5">Write your status here and click 'Status Set Using Legacy Api Call'</textarea> 
    <br /> 
    <a href="#" onClick="setStatus(); return false;">Status Set Using Legacy Api Call</a> 
</div> 
+0

코드에 JSON.stringify() 호출이 표시되지 않습니까? 스 니펫이 변경 되었습니까? – RAC

답변

2

당신은 json.stringify 여기에 대한 코드에 대한 링크를 찾을 수 있습니다 나는이 코드를 사용합니다. 당신이 사용하는 어떤 브라우저

<script src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script> 
+0

나는 그 링크를 열지 만, 내가 포함해야하는 코드를 얻을 수 없었다. 그리고 어디에서 – Deepa

+0

나는 질문을 업데이트했다 - 그것이 도움이되기를 바란다. – ipr101

+0

아직 해결되지 않은 오류가 없습니다 – Deepa

0

-

또는, 당신은 당신의 코드에이 스크립트에 대한 참조를 추가 할 수 있습니다? 모든 브라우저가 사용자가 시도하는 것처럼 기본 JSON 구문 분석을 지원하는 것은 아닙니다. 또한 다음과 같아야합니다.

JSON.stringify 
관련 문제