$.ajax({
type: "POST",
url: "127.0.0.1:8080/sendData",
data:data,
dataType: 'jsonp',
success: function(result) {
console.log("post successful")
},
error: function(result, status, error){
console.log("post unsuccessful");
console.log(result);
console.log(error);
}
});
这是我的代码。我有一个AngularJS应用程序,我可以从中调用此代码和一个可以监听的GoLang服务器。
当我从浏览器的控制台或控制器内执行此调用时,它不起作用 - 我得到404.这是图片
这是我的golang代码:
// ReceiveData - used to handle incoming data
func ReceiveData(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.NotFound(w, r)
return
}
vars := mux.Vars(r)
fmt.Println("ReceiveData function with data:")
fmt.Println(vars)
field := r.FormValue("textfield")
fmt.Println("textfield: ", field)
// database
session, err := mgo.Dial("localhost")
if err != nil {
panic(err)
} else {
fmt.Println("session created")
database := session.DB("schedule_calculator")
collection := database.C("schedule_save")
num, err := collection.Count()
if err == nil {
fmt.Println("schedule_save collection count = ", num)
} else {
fmt.Println("schedule_save error - ", err)
}
}
if err := json.NewEncoder(w).Encode("todos"); err != nil {
panic(err)
}
}
这是我的angularJS控制器:
controller: function($scope, $element, $attrs, $transclude) {
$scope.showSaveConfirmation = false;
$scope.saveSuccessful = false;
$scope.saveToDB = function() {
var data = $scope.gridData;
if (typeof data === 'object') {
data = JSON.stringify(data, undefined, 2);
$.ajax({
type: "POST",
url: "127.0.0.1:8080/sendData",
data:data,
dataType: 'jsonp',
success: function(result) {
console.log("post successful")
},
error: function(result, status, error){
console.log("post unsuccessful");
console.log(result);
console.log(error);
}
});
} else {
}
}
}
发生了什么事?谢谢
答案 0 :(得分:2)
如果你正在使用角度js,请尝试使用此js当前使用jquery进行ajax调用而不使用angular js
function sendData() {
$http({
url: 'request-url',
method: "POST",
data: { 'message' : message }
})
.then(function(response) {
// success
},
function(response) { // optional
// failed
});
}