javascript 带参数的 Angular JS AJAX 调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22651378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:31:11  来源:igfitidea点击:

Angular JS AJAX Call with Parameters

javascriptajaxasp.net-mvcangularjsparameters

提问by ElementaryStudentProgramming

without the parameters of the method Get, the code works, but if the method asks for a parameter an error 404 is returned. How do I properly send parameters with Angular JS?

如果没有 Get 方法的参数,代码可以工作,但如果该方法要求提供参数,则会返回错误 404。如何使用 Angular JS 正确发送参数?

factory.test = function () {
    var q = $q.defer();

    $http({
        method: "GET",
        url: url + "/dataEntry/test",
        data: {
            sampletext : "sample"
        }
    })
        .success(function (data, status, headers, config) { 
            q.resolve(data);
        })
        .error(function (data, status, headers, config) { 
            q.reject(data);
        });

    return q.promise;
}; 


    [Route("test")] 
    public String Get(string sampletext)
    { 
        return "Reply coming from data entry controller" + sampletext; 
    }

回答by Rowan Freeman

Since it's a GET request you shouldn't be sending data. You need to be sending a query string.

由于它是 GET 请求,因此您不应该发送数据。您需要发送一个查询字符串。

Change your datato params.

将您更改dataparams.

$http({
    method: "GET",
    url: url + "/dataEntry/test",
    params: {
        sampletext : "sample"
    }
})

Source: http://docs.angularjs.org/api/ng/service/$http

来源:http: //docs.angularjs.org/api/ng/service/$http

回答by Zeenat Pathan

$http({
    url: "/saveInfo",
    method: 'Post'
}).then(function(response) {
    console.log("saved successfully");
}, function(response) {
    console.log("Error message");   
});