您是否知道将Javascript对象编码为string可以通过GET请求传递的快速而简单的方法?
不jQuery,没有其他框架 - 只是简单的Javascript :)
谁能告诉我为什么以下声明不会将发布数据发送到指定的网址?当我打印$ _POST时,在服务器上调用url - 我得到一个空数组.如果我在将数据添加到数据之前在控制台中打印消息 - 它会显示正确的内容.
$http.post('request-url', { 'message' : message });
Run Code Online (Sandbox Code Playgroud)
我也尝试将数据作为字符串(具有相同的结果):
$http.post('request-url', "message=" + message);
Run Code Online (Sandbox Code Playgroud)
当我以下列格式使用它时它似乎正在工作:
$http({
method: 'POST',
url: 'request-url',
data: "message=" + message,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
});
Run Code Online (Sandbox Code Playgroud)
但有没有办法用$ http.post()来做 - 并且我总是必须包含标题才能使它工作?我相信上面的内容类型是指定发送数据的格式,但我可以将其作为javascript对象发送吗?
我有一个旧的Web应用程序,我必须支持(我没有写).
当我填写表格并提交然后检查Chrome中的"网络"标签时,我会看到"请求有效负载",我通常会看到"表单数据".两者之间有什么区别,何时会发送一个而不是另一个?
谷歌搜索这个,但没有找到任何解释这个的信息(只是人们试图让javascript应用程序发送"表单数据"而不是"请求有效负载".
我是AngularJS的新手,一开始,我想只使用AngularJS开发一个新的应用程序.
我正在尝试使用$http我的Angular App 对服务器端进行AJAX调用.
为了发送参数,我尝试了以下方法:
$http({
method: "post",
url: URL,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: $.param({username: $scope.userName, password: $scope.password})
}).success(function(result){
console.log(result);
});
Run Code Online (Sandbox Code Playgroud)
这是有效的,但它也使用jQuery $.param.为了消除对jQuery的依赖,我尝试了:
data: {username: $scope.userName, password: $scope.password}
Run Code Online (Sandbox Code Playgroud)
但这似乎失败了.然后我尝试了params:
params: {username: $scope.userName, password: $scope.password}
Run Code Online (Sandbox Code Playgroud)
但这似乎也失败了.然后我尝试了JSON.stringify:
data: JSON.stringify({username: $scope.userName, password: $scope.password})
Run Code Online (Sandbox Code Playgroud)
我找到了这些可能的答案,但没有成功.难道我做错了什么?我相信,AngularJS会提供这个功能,但是如何?
我需要将一个对象数组从我的Angular应用程序传递给一个带有Nancy框架的.Net Web服务.
我试过这个:
function TestCtrl($scope, $http){
$scope.postTest = function(){
var data = [obj1, obj2, obj3];
$http({
url: 'myURL',
method: "POST",
data: data,
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
}).success(function(data){
alert("done");
});
}
}
Run Code Online (Sandbox Code Playgroud)
但服务器发送500内部服务器错误.
我不知道为什么它不起作用.我不是专家的网络服务专家,但我认为这是一个序列化问题.
有人能帮我吗?
在我使用JQuery之前,我使用它来发送带参数的URL
window.location = myUrl + $.param({"paramName" : "ok","anotherParam":"hello"});
Run Code Online (Sandbox Code Playgroud)
但是对于angularjS,这不起作用
$scope.myButton = function() {
$window.location.open = myUrl + $.param({"paramName" : "ok","anotherParam":"hello"});
};//Error: $ is not defined
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我在angularJs中做到这一点
我试着这样:
$http({ method: 'POST', url: '/token', data: { username: $scope.username, password: $scope.password, grant_type: 'password' } }).success(function (data, status, headers, config) {
$scope.output = data;
}).error(function (data, status, headers, config) {
$scope.output = data;
});
Run Code Online (Sandbox Code Playgroud)
然后尝试将grant_type更改为param:
$http({ method: 'POST', url: '/token', data: { username: $scope.username, password: $scope.password }, params: { grant_type: 'password' } }).success(function (data, status, headers, config) {
$scope.output = data;
}).error(function (data, status, headers, config) {
$scope.output = data;
});
Run Code Online (Sandbox Code Playgroud)
仍然可怕: {"error":"unsupported_grant_type"}
所以我做了AngularJS开发人员应该做的事情,使用jQuery:
var data = $('#regForm').serialize() + "&grant_type=password"; …Run Code Online (Sandbox Code Playgroud) javascript jquery asp.net-web-api angularjs asp.net-web-api2
我想在angularjs中更改post ['Content-Type'],所以我使用
app.config(function($locationProvider,$httpProvider) {
$locationProvider.html5Mode(false);
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
});
Run Code Online (Sandbox Code Playgroud)
事件是
$http.post("http://172.22.71.107:8888/ajax/login",{admin_name:user.u_name,admin_password:user.cert})
.success(function(arg_result){
console.log(arg_result);
});
};
Run Code Online (Sandbox Code Playgroud)
然而,结果是
Parametersapplication/x-www-form-urlencoded
{"admin_name":"dd"}
Run Code Online (Sandbox Code Playgroud)
我想要的是什么
Parametersapplication/x-www-form-urlencoded
admin_name dd
Run Code Online (Sandbox Code Playgroud)
那我该怎么办?
我正在学习AngularJS并尝试构建从Wordpress获取数据的前端系统.
在后端方面,一切似乎都设置正确,当我使用jQuery ajax请求时,它可以毫无问题地获取数据.
jQuery.ajax({
type: 'POST',
url: '/wp-admin/admin-ajax.php',
data: {
action: 'getdataajax'
},
success: function(data, textStatus, XMLHttpRequest){
console.log(data);
},
error: function(MLHttpRequest, textStatus, errorThrown){
console.log(errorThrown);
}
});
Run Code Online (Sandbox Code Playgroud)
但是当我尝试用AngularJS做同样的事情时,它不起作用.我试图用这样的代码复制ajax请求:
myApp.factory('productsData', function($http, $log) {
return {
getProducts: function(successcb) {
return $http({
method: 'POST',
url: '/wp-admin/admin-ajax.php',
data: {action: 'getdataajax'}
}).success(function(data, status, headers, config) {
successcb(data);
$log.info(data, status, headers(), config)
}).error(function(data, status, headers, config) {
$log.warn(data, status, headers(), config)
});
},
};
});
Run Code Online (Sandbox Code Playgroud)
如果我记录它,它输出0.我缺少什么?
谢谢你的帮助.
PS控制器看起来像这样:
myApp.controller('ProductsController', function ProductsController($scope, productsData) {
$scope.sortorder = 'name';
// …Run Code Online (Sandbox Code Playgroud) 如何从Google App Engine WebApp2中的Angular POST请求中获取数据? self.request.body返回一个字符串,不self.request.get(key)返回任何内容.
提交POST的Angular代码是:
$http.post("/mail", {request_name: 'Test Name', request_body: 'Test Body'});
Run Code Online (Sandbox Code Playgroud)
然后我的WebApp2处理程序中的这两行:
print "1: " + self.request.body
print "2: " + self.request.get('request_name')
Run Code Online (Sandbox Code Playgroud)
打印此:
1: {"request_name":"Test Name","request_body":"Test Body"}
2:
Run Code Online (Sandbox Code Playgroud)
从POST正文中获取数据的最佳方法是什么?或者我应该以不同方式发送请求?
angularjs ×8
javascript ×5
jquery ×4
post ×3
ajax ×2
http ×2
rest ×2
angular-http ×1
forms ×1
param ×1
query-string ×1
url ×1
urlencode ×1
webapp2 ×1
wordpress ×1