1

Here i have small login created using Ajax XML. its work fine.

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

I want to integrate this same process with Angulajs. will this work with AngularJS or do i need to remodify this to AngularJS process. i can find reference that do just URL Ajax request using angularjs. but couldnt find with parameters that i want to send. username and Password

found this in AngularJS site

// 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.
  });

Sorry for english

1 Answer 1

2

You are in the right way. You can pass parameter to Angular $http like in the function below:

$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.
});
Sign up to request clarification or add additional context in comments.

5 Comments

I think you want to use data: { username: "", password: "" } instead of params or your password will appear in the URL (and logs!).
ah thanks, so will this work with just triggering function using onClick or onsubmit?
It's supposed to do so!
@CadeLewis note that by degault $http sends application:json not application/x-www-form-urlencoded, which you were doing with raw XHR.
@Musa how can i change it to Content-Type':'application/x-www-form-urlencoded ? with out this it wont work right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.