如何使用JavaScript安全地对URL进行编码,以便将其放入GET字符串?
var myUrl = "http://example.com/index.html?param=1&anotherParam=2";
var myOtherUrl = "http://example.com/index.html?url=" + myUrl;
Run Code Online (Sandbox Code Playgroud)
我假设你需要myUrl在第二行编码变量?
在下面的代码中,AngularJS $http方法调用URL,并将xsrf对象作为"请求有效负载"提交(如Chrome调试器网络选项卡中所述).jQuery $.ajax方法执行相同的调用,但将xsrf提交为"Form Data".
如何让AngularJS将xsrf作为表单数据而不是请求有效负载提交?
var url = 'http://somewhere.com/';
var xsrf = {fkey: 'xsrf key'};
$http({
method: 'POST',
url: url,
data: xsrf
}).success(function () {});
$.ajax({
type: 'POST',
url: url,
data: xsrf,
dataType: 'json',
success: function() {}
});
Run Code Online (Sandbox Code Playgroud) 使用javascript如何在网址中添加查询字符串参数(如果不存在)或是否存在,更新当前值?我正在使用jquery进行客户端开发.
我正在尝试查找有关如何序列化对象以查询字符串格式的信息,但我的所有搜索都在结果中淹没了如何以其他方式(字符串/表单/任何JSON).
我有
{ one: 'first', two: 'second' }
Run Code Online (Sandbox Code Playgroud)
而且我要
?one=first&two=second
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?我不介意插件或其他东西 - 如果我发现的代码不是插件,我可能会重新写一个...
只是想知道是否有任何内置的Javascript可以获取一个Form并返回查询参数,例如:"var1 = value&var2 = value2&arr [] = foo&arr [] = bar ..."
多年来我一直在想这个.
有没有办法在JavaScript中创建用于执行GET请求的查询参数?
就像在Python中一样urllib.urlencode(),它接收字典(或两个元组的列表)并创建一个类似的字符串'var1=value1&var2=value2'.
我有一个带标签的表格 ng-submit="login()
该函数在javascript中被称为fine.
function LoginForm($scope, $http)
{
$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
$scope.email = "fsdg@sdf.com";
$scope.password = "1234";
$scope.login = function()
{
data = {
'email' : $scope.email,
'password' : $scope.password
};
$http.post('resources/curl.php', data)
.success(function(data, status, headers, config)
{
console.log(status + ' - ' + data);
})
.error(function(data, status, headers, config)
{
console.log('error');
});
}
}
Run Code Online (Sandbox Code Playgroud)
我从PHP文件中得到200 OK响应,但是,返回的数据是这样email并且password未定义.这是我的所有PHP
<?php
$email = $_POST['email'];
$pass = $_POST['password'];
echo $email;
?>
Run Code Online (Sandbox Code Playgroud)
知道为什么我得到未定义的POST值吗?
编辑
我想指出,因为这似乎是一个受欢迎的问题(但它已经过时了),.success并且.error已经被弃用了,你应该使用 …
我正在尝试使用youtube数据api生成视频播放列表.但是,视频网址需要youtube.com/watch?v=3sZOD3xKL0Y格式,但api生成的是youtube.com/watch?v=3sZOD3xKL0Y&feature=youtube_gdata.所以我需要做的是能够选择&符号之后的所有内容并将其从url中删除.使用javascript和某种正则表达式的任何方式吗?
我需要将一个对象数组从我的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内部服务器错误.
我不知道为什么它不起作用.我不是专家的网络服务专家,但我认为这是一个序列化问题.
有人能帮我吗?
javascript ×8
angularjs ×3
jquery ×3
query-string ×3
post ×2
url ×2
ajax ×1
angular-http ×1
arrays ×1
encoding ×1
expression ×1
forms ×1
get ×1
http ×1
json ×1
php ×1
rest ×1
string ×1
urlencode ×1