Javascript Angular js$http post不工作
我正在使用http post通过以下代码从Web服务获取数据Javascript Angular js$http post不工作,javascript,jquery,angularjs,http-post,Javascript,Jquery,Angularjs,Http Post,我正在使用http post通过以下代码从Web服务获取数据 $http({ method: "post", url: "SurveyQuestions.asmx/GetSurveyQuestions", data: JSON.stringify({"empl_id" : '12345' , "Year" : '2015' }), headers: { 'Content
$http({
method: "post",
url: "SurveyQuestions.asmx/GetSurveyQuestions",
data: JSON.stringify({"empl_id" : '12345' , "Year" : '2015' }),
headers: { 'Content-Type': 'application/json' },
dataType: "json",
contentType: "application/json; charset=utf-8",
}).success(function (response) { //do something})
上面的代码使用不带参数的“get”运行良好
我的网站服务是
<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=False)> _
Public Sub GetSurveyQuestions(ByVal Empl_id as string, ByVal Year as string)
Try
//get list of records
Dim js As JavaScriptSerializer = New JavaScriptSerializer
Context.Response.Write(js.Serialize(listSurveyQuestions))
Catch ex As Exception
End Try
End Sub
_
_
Public Sub GetSurveyQuestions(ByVal emp_id作为字符串,ByVal Year作为字符串)
尝试
//获取记录列表
Dim js As JavaScriptSerializer=新JavaScriptSerializer
Context.Response.Write(js.Serialize(listSurveyQuestions))
特例
结束尝试
端接头
Web服务正在正常执行并将数据返回给方法,但是
$http
中的响应为空,并且出错 您只需尝试传递仅在最后一个url中添加的对象
在我的例子中,我只是添加员工Id。如果只发送员工对象,它也会正确
$scope.removeEmployee = function(employee) {
var method = 'DELETE';
var url = 'http://localhost:8080/AngularJSWithRestful/rest/product/Delete/' + employee.id ;
$http({
method : method,
url : url,
}).then(function successCallback(response) {
$scope.product = response.data.product;
}, function errorCallback(response) {
});
};
因为您正在检索数据,并且服务器上没有执行任何更改(根据您的评论),所以最好将其更改为HttpGet方法。然后在angular中,您应该在传递给$http函数的对象中使用
params
属性。这是因为HttpGet
不支持消息正文。从:
params–{Object.}
–将使用paramSerializer序列化并作为GET参数追加的字符串或对象的映射
您更改的代码:
$http({
method: "GET",
url: "SurveyQuestions.asmx/GetSurveyQuestions",
params: {"empl_id" : '12345' , "Year" : '2015' }, // use the params property
headers: { 'Content-Type': 'application/json' },
dataType: "json",
contentType: "application/json; charset=utf-8",
}).then(function (response) { //do something})
还要注意,angular团队将成功和错误标记为过时。建议使用然后
。请参见相同的文档链接
弃用通知书
$http legacy promise方法success和error已被弃用。改用标准then方法。如果$httpProvider.useLegacyPromiseExtensions设置为false,则这些方法将抛出$http/legacy错误
另请参见错误是什么?@CharlieH意外的标记{位于Object.parse(native)的位置3483处的JSON中。仅供参考,当我删除参数并更改为“get”时,它会工作。相同的代码更改为“post”会引发错误。@CharlieH它正在工作。我不知道,我可以使用“get”并在URL本身中包含参数。谢谢。