[英]jQuery Ajax request turn into AngularJS $http request
I have such jQuery Ajax request. 我有这样的jQuery Ajax请求。 I need to turn it into an AngularJS $http request. 我需要将其转换为AngularJS $ http请求。 How can I do that? 我怎样才能做到这一点?
$.ajax({
type: "POST",
dataType: "jsonp",
crossDomain: false,
data:{
user: username,
pass: password
},
beforeSend: function (request){
request.setRequestHeader("Accept-Language", languageCode);
},
url: someUrl
})
.done(function (data, textStatus, jqXHR){
console.log(data);
})
.fail(function (jqXHR, textStatus){
console.log("failed");
});
My Angular Implementations 我的角度实现
Response {"data" : "", "status" : "200", "config" : .... } Data is empty 响应{“ data”:“”,“ status”:“ 200”,“ config”:....}数据为空
login : function (username, password) {
return $http({
method: 'POST',
url: someUrl,
data: {
user: username,
pass: password
},
headers: {
'Accept-Language': languageCode
}
})
}
I succeeded to get data wrapped in JSON_CALLBACK with next request. 我成功通过下一个请求将数据包装在JSON_CALLBACK中。 But I don't know how to call that callback. 但是我不知道如何调用该回调。 It is not done automatically. 它不会自动完成。 Response looks like {data:"JSON_CALLBACK({mydata})"...} 响应类似于{data:“ JSON_CALLBACK({mydata})” ...}
login : function (username, password) {
var url = someUrl
+ '?callback=JSON_CALLBACK&user=' + username
+ '&pass=' + password;
return $http.post(url);
},
Please refer to $http documentation. 请参考$ http文档。 but here is somewhat counterpart for that jquery request 但是这与那个jquery请求相对应
$http({
method: 'POST',
url: '/someUrl',
data: {
user: username,
pass: password
},
headers: {
'Accept-Language':languageCode
}
}).then(function successCallback(response) {
//do something
}, function errorCallback(response) {
//do something
});
There is no such thing as a JSONP POST request. 没有JSONP POST请求之类的东西。 When jQuery sees dataType: jsonp
, it ignores the method
propery and uses a script GET request. 当jQuery看到dataType: jsonp
,它将忽略method
属性,并使用脚本GET请求。 The data is added as parameters of the URL. 数据被添加为URL的参数。
In AngularJS, JSONP requests are specified with method: 'JSONP
. 在AngularJS中,JSONP请求使用method: 'JSONP
指定。 The callback parameter needs to be JSON_CALLBACK
and must be capitalized. callback参数必须为JSON_CALLBACK
并且必须大写。
login : function (username, password) {
return $http({
//method: 'POST',
method: 'JSONP',
url: someUrl,
//data: {
params: {
user: username,
pass: password,
callback: "JSON_CALLBACK"
},
headers: {
'Accept-Language': languageCode
}
})
}
Be aware that since the data is sent as URL parameters, sensitive information such as passwords are vulnerable to snooping attacks. 请注意,由于数据是作为URL参数发送的,因此敏感信息(例如密码)容易受到监听攻击。
To demonstrate on JSFiddle: 在JSFiddle上进行演示:
var url="//jsfiddle.net/echo/jsonp/";
var params = { user: "myUser",
pass: "123456",
callback: "JSON_CALLBACK"
};
$http.jsonp(url, { params: params} )
.then(function onSuccess(response) {
$scope.response = response;
console.log(response);
})
The DEMO on JSFiddle . JSFiddle上的DEMO 。
AngularJS error .success is not a function AngularJS错误.success不是函数
Might have something to do with it?? 可能与它有关吗?
What API are you working with? 您正在使用什么API?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.