2015-02-13 3 views
1

Здесь у меня есть небольшой вход, созданный с использованием Ajax XML. его работа прекрасна.Javascript Ajax будет работать с AngularJS?

function ajax_post(){ 
     // Create our XMLHttpRequest object 
     var hr = new XMLHttpRequest(); 
     // Create some variables we need to send to our PHP file 
     var url = "http://boost.meximas.com/mobile/login.php"; 
     var fn = document.getElementById("username").value; 
     var ln = document.getElementById("password").value; 
     var vars = "username="+fn+"&password="+ln; 
     hr.open("POST", url, true); 
     // Set content type header information for sending url encoded variables in the request 
     hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
     // Access the onreadystatechange event for the XMLHttpRequest object 
     hr.onreadystatechange = function() { 
     if(hr.readyState == 4 && hr.status == 200) { 
      var return_data = hr.responseText; 
     //document.getElementById("status").innerHTML = return_data; 
     if(return_data=="1"){ 

      location.href = "home.html?username=" + fn; 
     }else{ 
      //alert("Login Failed..."); 
      $.mobile.loading("hide"); 
      ons.notification.alert({message: 'Login Failed!'}); 

     } 

     } 
     } 
     // Send the data to PHP now... and wait for response to update the status div 
     hr.send(vars); // Actually execute the request 

Я хочу интегрировать этот процесс с Angulajs. будет ли это работать с AngularJS или мне нужно переделать это в процесс AngularJS. Я могу найти ссылку, которая делает только запрос URL Ajax, используя angularjs. но не смог найти с параметрами, которые я хочу отправить. имя пользователя и пароль

нашел в AngularJS сайте

// Simple GET request example : 
$http.get('/someUrl'). 
    success(function(data, status, headers, config) { 
    // this callback will be called asynchronously 
    // when the response is available 
    }). 
    error(function(data, status, headers, config) { 
    // called asynchronously if an error occurs 
    // or server returns response with an error status. 
    }); 

Извините за английский

ответ

2

Вы находитесь на правильном пути. Вы можете передать параметр Angular $ http, как в функции ниже:

$http({ 
    url: "http://boost.meximas.com/mobile/login.php", 
    method: "POST", 
    data: { username: "", password: "" }, 
    headers: {'Content-Type': 'application/x-www-form-urlencoded'} 
}).success(function(data, status, headers, config) { 
    // this callback will be called asynchronously 
    // when the response is available 
}). 
error(function(data, status, headers, config) { 
    // called asynchronously if an error occurs 
    // or server returns response with an error status. 
}); 
+0

Я думаю, вы хотите использовать 'data: {имя пользователя:" ", пароль:" "}' вместо 'params' или ваш пароль будет отображаются в URL-адресе (и в журналах!). – Jasen

+0

Да ... моя ошибка! Починил это. Спасибо – nanndoj

+0

ах спасибо, так будет ли это работать только с функцией запуска, используя onClick или onsubmit? –