Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
82 views
in Technique[技术] by (71.8m points)

javascript - I failed to get data from js through ajax to php

I failed to get data from below js through ajax to php, is there someone who can help me to solve his problem?

// Testing Graph API after login.
// See statusChangeCallback() for when this call is made.
function testAPI() {

  console.log('Welcome! Fetching your information...');

  FB.api('/me', function(response) {
    console.log('Successful login for: ' + response.name);
    document.getElementById('status').innerHTML = '' + response.name + '!'; 
            
    //ajax used to get the name from response.name to php
    $.ajax({
      type: 'POST',
      url: 'result.php',
      data: { name: response.name },
      success: function (msg) {
        /* do something */
      }   
    });

  });

} 

And in php file I'm getting data like below when I click submit button

<?php
$name=$_GET['name'];
echo"$name";
?>

I failed and I used include("result.php"); But no data displayed

Actually response.name have name value but I failed to get it in php variable

Please anyone can help me

question from:https://stackoverflow.com/questions/65896639/i-failed-to-get-data-from-js-through-ajax-to-php

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Change your js file to this:

function testAPI() {                     
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {
      console.log('Successful login for: ' + response.name);
      document.getElementById('status').innerHTML =
        '' + response.name + '!';
        $.ajax({
        type: 'POST',
        url: 'result.php',
        data: {name: response.name },
        success: function(msg) {
                console.log(msg);
            }   
        });
    });
} 

And change your php file to this:

<?php
 print_r($_POST);
?>

and then go to the console and search for the name variable, if you can find the name variable then all things is fine.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...