2016-03-04 82 views
0

我有发送POST请求到指定的RESTful服务,如果用户名和密码是正确的一个形式,它会返回翻译和输出AJAX对象请求

Object {status: "success", message: "Welcome!"} 

,如果有错误将返回

Object {status: "error", message: "Incorrect username or password."} 

都在控制台通过

console.log(data) 

是有办法,我可以把这些Ø对象并访问它们并为每个响应做一些事情,例如显示元素等。

请在下面找到我的代码。

$(document).ready(function() { 

    $("#login-form").submit(function(e) { 
     e.preventDefault(); 
     var formData = { 
      'username': $('#username').val(), 
      'password': $('#password').val(), 
     }; 
     $.ajax({ 

      type: 'POST', 

      url: 'URL HERE', 

      data: formData, 

      success: function(data) { 
       // Here's where you handle a successful response. 
       console.log(); 
      }, 

      error: function() { 
       $('#error').show().text("There seems to be a problem logging in."); 
      } 
     }) 



     // using the done promise callback 
     .done(function(data) { 

      // log data to the console so we can see 
      console.log(data); 

      // here we will handle errors and validation messages 
     }); 

     // stop the form from submitting the normal way and refreshing the page 
     e.preventDefault(); 


    }); 
}); 


<form name="login-form" id="login-form" method="post" action="URL HERE"> 
         <label id="error">Sorry it seems your credentials are incorrect</label> 
         <div class="inputBlock"> 
          <label for="username">Username</label> 
          <input type="text" id="username" name="username"/> 
         </div> 
         <div class="inputBlock"> 
          <label for="password">Password</label> 
          <input type="password" id="password" name="password"/> 
         </div> 
         <input type="submit" value="Login" id="submit" /> 
        </form> 

任何帮助将是太棒了!

回答

2

您可以使用JavaScript

//In your done function just treat "data" like an object 
    .done(function(data) { 

     if(data.status === "success"){ 
      //Do something 
     } 
     else if (data.status === "error"){ 
      //Display error 
     } 
    }); 
+0

太谢谢你了! –

+0

如果我想得到消息字符串,我将如何去输出这个? –

+0

@HarryBerry你可以通过'data.message'来访问消息,但最终取决于你想显示它的位置,如果你试图将它写入网页,你需要操作DOM以显示值该网页,如果你只是想把它记录到控制台,你可以做'console.log(data.message)' – tt9

0

检查返回的对象的属性,这是我下面的新代码。

$(document).ready(function() { 

    $("#login-form").submit(function(e) { 
     var formData = { 
      'username': $('#username').val(), 
      'password': $('#password').val(), 
     }; 

     $.ajax({ 
      type: 'POST', 
      url: 'http://www2.opticalexpress.co.uk/test/login.php', 
      data: formData, 
      success: function(data) { 
       // Here's where you handle a successful response. 
       console.log(); 
      }, 
      error: function() { 
       $('#error').show().text("There seems to be a problem logging in."); 
      } 
     }) 
     // using the done promise callback 
     .done(function(data) { 
      // log data to the console so we can see 
      console.log(data); 
      if(data.status === "success"){ 
      alert("success"); 
     } 
     else if (data.status === "error"){ 

      var errorMsg = $(data.message); 
       $('body').append("<p>" + errorMsg + "</p>"); 
     } 
      // here we will handle errors and validation messages 
     }); 
     // stop the form from submitting the normal way and refreshing the page 
     e.preventDefault(); 
    }); 
}); 

错误我收到是

Uncaught Error: Syntax error, unrecognized expression: Incorrect username or password.