Ajax调用返回空API响应

时间:2017-11-10 17:31:02

标签: javascript php jquery ajax

我使用HTML表单和带有Ajax调用的PHP代码在API URL上发布电子邮件。但回应是空的。我想在表单下的同一页面上打印响应。我在这里发布我的所有代码,想知道我在做什么错。目前我正在使用我的localhost XAMP服务器,这也很重要吗?

我的HTML

<form method="post">
          <div class="col-md-7 col-sm-10 col-xs-12 mid-align">
            <div class="input-group margin-top-35 mid-align">
              <input type="email" name="email" class="mob-row form-control text-box" id="email" placeholder="Enter your email address">
              <span class="input-group-btn">
                <button class="btn btn-default join" type="submit">JOIN US</button>
              </span>
            </div><!-- /input-group -->
          </div>
        </form>

我的PHP代码

header('Content-Type: application/json');
$email = trim($_POST['email']);

    $url = 'https://slack.com/api/users.admin.invite?token=$token&email='.$email.'';

    $response = file_get_contents($url);
    echo $response;

我的Ajax电话

    $(document).ready(function() {
    var e = $(".error");
    $(".join").click(function() {
        var a = $("#email").val();
        $.ajax({
            type: "POST",
            url: "post-data.php",
            dataType: "json",
            data: {
                email: a
            },
            error: function(e) {
                console.log(response);
                },
            success: function(a) {
            console.log(response);
                a.ServiceOperationResult.success ? e.hide() : e.html(a.ServiceOperationResult.messageText).show()
            },

        })
    })
})

5 个答案:

答案 0 :(得分:0)

表单已提交并刷新页面,因此ajax调用未执行。我建议你删除标签,然后再试一次。这次ajax将开始工作,你可以使用console.log();

进行检查

答案 1 :(得分:0)

  1. 从表单标记中删除帖子

  2. 将ajax数据元素中的键保存在单引号中

  3. 纠正您的console.log语句

  4. 确保在您的php文件中定义$ token是字段

答案 2 :(得分:0)

两个完全不同的问题可能有相同的答案,两者中没有一个可以是另一个的副本。

就像这里一样,显然将链接返回到导致相同答案的第一个问题的原则似乎是禁止的,我在这里放置了我自己用来在浏览器和服务器之间发送和接收JSON数据的相同代码副本PHP。

我已经使用了很多年了,随着时间的推移,这段代码得到了改进,缓存管理出现了明显的问题,这可能是某些浏览器的问题(甚至看到jQuery的版本,但是我不要发起这种问题),对于那些想继续怀疑这个问题的有效性的人,我建议他们在google上搜索以下lerms:&#34; jquery ajax问题json cache&#34 ;意识到在这个问题上一切都不一定如此简单。

最后,我认为这段代码完全可靠,我将继续在相同的状态下使用它。

没有什么可以阻止你删除将缓存置为false的代码行;就我而言,我不会狡辩这个细节。

所以,这是代码:&#34;我使用jQuery-ajax和php的方式#34;

jQuery Part :( JSON比我们想象的更简单?)

    var 
        Call_Args = {
            what:  $('#id_what').val(),
            why:  $('#id_why').val(),
            who:  $('#id_who').val() 
        },
        answer_1 = '',
//      ...
        answer_n = '';

        $.ajax({
            url: 'assets/php/the_Called_PHP_Function.php',  // path is from html file
            type: 'POST',
            data: Call_Args,
            cache: false,
            dataType: 'json',
            error: function (request, error) {
                alert("Error : responseText: " + request.responseText); // don't mind, it's never happened
            },
            success: function (data) {
                answer_1 = data['rep_1'];
                answer_n = data['rep_n'];

                // your javascript stuff here... remember, ajax is asynchrone...
            } 

        });

php part(&#39; assets / php / the_Called_PHP_Function.php&#39;)

<?php
mb_internal_encoding("UTF-8");   // !important, characters have many awards for stupid traps

    $ref_what   = (isSet($_POST['what']))       ? $_POST['what']        : '';
    $ref_why        = (isSet($_POST['why']))        ? $_POST['why']     : '';
    $ref_who        = (isSet($_POST['who']))        ? $_POST['who']     : '';

// your php code stuff here...

    $T_Repons['rep_1']          = 'bla bla bla';
// ...
    $T_Repons['rep_n']          = 'turlututu...';


header('Content-type: application/json');
echo json_encode($T_Repons);
exit(0);
?>

答案 3 :(得分:0)

将按钮类型提交更改为按钮,因为您的表单将使用提交按钮提交。

型=&#34;提交&#34;

型=&#34;按钮&#34;

答案 4 :(得分:-1)

我希望你是jokn兄弟,首先:你没有定义$ token,scnd:你使用'但你需要使用'来解析变量,如“https://slack.com/api/users.admin.invite?token= $ token&amp; email =”。$ email。“或者像'https://slack.com/api/users.admin.invite?token='。$ token。'&amp; email ='。$ email。',thrd:如果你写成功,你需要使用变量a作为响应:function(a),以及更多其他内容你的代码,sry for my ez eng,我快速写在手机上

相关问题