jquery POST响应成功但数据为空

时间:2016-01-31 16:18:07

标签: javascript jquery html post response

我正在使用jquery将帖子发送到我的api的会话端点以获取登录令牌。请求返回成功,但数据为空。如果我在cURL中执行相同的请求,则返回json。 我是jquery的新手。有没有人对这个问题有什么建议?

<!DOCTYPE html>
<HTML> 
     <HEAD>
      <TITLE>  login </TITLE>      
    </HEAD>
    <LINK rel="stylesheet" type="text/css" href="login.css" />
  <BODY>
    <DIV id="container">
      <DIV id="header">
        <IMG src="image.bmp" alt="logo" style="width:64px;height:64px;"/>
        <H1> my service </H1>
      </DIV>
      <DIV id="content"> 
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
      <script>
       $(document).ready(function()
       {
         $("button").click(function()
         {
           $.post("www.mydomain.com/api/session",
          {
            username: "batman",
            password: "123"
          },
        function(data,status)
        {
            alert("Data: " + data + "\nStatus: " + status);
        });
    });
});
</script>   
             <DIV id="loginform">
               <form  method="POST">
                  Username: <input type="text" name="username" /><br />
                  Password: <input type="password" name="password" /><br />
                  <button> welcome! </button>
               </form>
             </DIV>
          <DIV id="description">
            <P>mydomain description</P>
            <P>Join now to see what people are doing near you!</P>
          </DIV> 
          <DIV id="register">
            <P>Dont have an account with us? </P>
            <P>Register here now !!</P>
          </DIV> 
       </DIV>   
          <DIV id="nav">
        </DIV>
      </DIV>
    </DIV>
  </BODY>
</HTML>

4 个答案:

答案 0 :(得分:2)

WOW!我发现了问题,这很简单。我只需要在我的网址前面使用http://。 感谢每一个时间和帮助!。

答案 1 :(得分:0)

此外,Punit的答案,我建议使用优于.click()函数的$.on()方法。

$(function() {
// equals to $(document).ready(function(){ but faster to wright
  $("#loginform").on('click', 'button', function() {
      //Punit's Code (that is good !!)
      $.ajax({ url: 'your_url_here',
           data: {username: 'batman' , password : "123"},
           type: 'post',
           dataType: "json",
           success: function(output) {
                        alert(output);
                    }
      });
  });
});

答案 2 :(得分:0)

问题可能是您需要在执行按钮单击事件后停止提交表单。如上所述,一旦脚本完成,页面就会刷新,并且没有机会处理发布请求的结果。从点击功能返回'false':

   $("button").click(function() {
       $.post("www.mydomain.com/api/session",
      {
        username: "batman",
        password: "123"
      },
      function(data,status)
      {
          alert("Data: " + data + "\nStatus: " + status);
      });
      return false;
    }    
   );

这样做你应该看看你的帖子请求是否返回任何数据。请注意,因为您要发布到目录,所以该脚本将默认查找索引文件,例如index.php文件。如果这是一个问题,请将您的URL更改为直接指向相关的服务器端脚本。

答案 3 :(得分:-1)

以下是创建Ajax调用的方法,现在您需要执行此操作。你必须从URL返回数据到json格式,然后在success函数中尝试console.log(输出),你会发现它很热的想法。

$.ajax({ url: 'your_url_here',
             data: {username: 'batman' , password : "123"},
             type: 'post',
             dataType: "json",
             success: function(output) {
                          alert(output);
                      }
    });

如果你不懂任何地方,请告诉我,

相关问题