简体   繁体   中英

Ajax request said successful submitted data but php does not respond

I am working on a facebook login typo3 plugin for typo3. I use the following code to send a request to a php page.

FB.getLoginStatus(function(response) {

      if (response.status === 'connected') {

        console.log(JSON.stringify(response));

        jQuery.ajax({
            url : "/index.php?id=379",
            type: "POST",
            data : "{accessToken:" + "'"+ response.authResponse.accessToken + "', userID:" + "'" + response.authResponse.userID + "'}",
            success: function(data, textStatus, jqXHR)
            {
                console.log("Data submitted");
                console.log(data);
            },
            error: function (jqXHR, textStatus, errorThrown)
            {
                console.log("Data not submitted");
            }
        }); 
      }
}); 

I get "Data submitted" as a response, but my php-website does not response with "got it". ###TESTER### is a marker for the frontend. The text got it should be displayed in the frontend:

function main($content, $conf) { 

$data = $_POST['data'] or $_REQUEST['data'];
        $markerArray['###TESTER###'] = '';

    if(!empty($data)) {
        $markerArray['###TESTER###'] = "got it";
    }

Your PHP code is embedded in the main function, the main function will not be run unless it is called in the PHP page.

Another problem is that in your AJAX request you do not set the following POST variables: "accessToken", "userId" but there is no "data" variable which you are looking for in the PHP page which you are calling.

Thank your for your reply. I figured out an other way. I use windows.location to submit the accessToken in the url. Not the best way but it works.

FB.login(function(response) {
       if (response.authResponse) {
         var access_token =   FB.getAuthResponse()['accessToken'];
         var userID =   FB.getAuthResponse()['userID'];

            FB.api('/me', function(response) {
              console.log('Erfolgreiche Anmeldung für: ' + response.name);

              document.getElementById('status').innerHTML =
                'Danke für die Anmeldung, ' + response.name + '!';

                 var location = "/index.php?id=379&accessToken=" + access_token + "&userID=" + userID;
                 console.log(location);
                 window.location = location;
            });
       } else {
             console.log('User cancelled login or did not fully authorize.');
           }
}, {scope: ''});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM