I'm trying to connect my html with MongoDB. The data are successfully entered, but the ajax won't start. I'm going to make sure what i enter is in my database (a login form). Here's my code:
.controller('LoginCtrl', function($scope, $state, $location, $ionicPopup) {
$scope.login = function(data) {
var formdata = {
phone : $("#phone").val(),
pin : $("#pin").val()
};
var Jformdata = JSON.stringify(formdata);
console.log(Jformdata);
$.ajax({
url : "/SinarmasProject/Submit",
context : document.body,
type : 'POST',
data : Jformdata,
contentType : "application/json; charset=utf-8",
}).done(function (response){
if(response == "true"){
$location.path('/login/main/dash');
}else if(response == "false"){
$scope.showAlert('Nomer Telephone dan PIN Salah');
}
});
}
})
The error turns out like this:
POST http://localhost:8089/SinarmasProject/Submit 500 () jquery-3.1.0.min.js:4
send @ jquery-3.1.0.min.js:4
ajax @ jquery-3.1.0.min.js:4
$scope.login @ controllers.js:41
$parseFunctionCall @ ionic.bundle.js:20628
(anonymous function) @ ionic.bundle.js:52854
$eval @ ionic.bundle.js:22684
$apply @ ionic.bundle.js:22783
(anonymous function) @ ionic.bundle.js:52853
eventHandler @ ionic.bundle.js:11297
triggerMouseEvent @ ionic.bundle.js:2863
tapClick @ ionic.bundle.js:2852
tapMouseUp @ ionic.bundle.js:2922
EDIT: My controller.java
package com.spring.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.spring.beans.BankBean;
@Controller
public class controller {
@Autowired
private MongoTemplate mongotemplate;
@RequestMapping(value="/BankMasuk", method= {RequestMethod.GET})
//@ResponseBody
public List<BankBean> BankMasuk(){
List<BankBean> BankBean = mongotemplate.find(new Query(), BankBean.class);
return BankBean;
}
@RequestMapping(value="/Submit", method={RequestMethod.POST})
@ResponseBody
public String SubmitBtn(@RequestBody BankBean bank){
String temp;
String string1 = bank.getPhone();
String string2 = bank.getPin();
Query query = new Query();
query.addCriteria(Criteria.where("phone").is(string1));
BankBean person = mongotemplate.findOne(query, BankBean.class);
if(string1.equals(person.getPhone()) && string2.equals(person.getPin())){
temp = "true";
}else{
temp="false";
}
return temp;
}
}
EDIT: BankBean
package com.spring.beans;
public class BankBean {
public String phone;
public String pin;
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPin() {
return pin;
}
public void setPin(String pin) {
this.pin = pin;
}
@Override
public String toString() {
return "BankBean [phone=" + phone + ", pin=" + pin + "]";
}
}