我想从jQuery向AJAX POST请求添加自定义标头.
我试过这个:
$.ajax({
type: 'POST',
url: url,
headers: {
"My-First-Header":"first value",
"My-Second-Header":"second value"
}
//OR
//beforeSend: function(xhr) {
// xhr.setRequestHeader("My-First-Header", "first value");
// xhr.setRequestHeader("My-Second-Header", "second value");
//}
}).done(function(data) {
alert(data);
});
Run Code Online (Sandbox Code Playgroud)
当我发送此请求并使用FireBug观看时,我看到此标题:
选项xxxx/yyyy HTTP/1.1
主机:127.0.0.1:6666
User-Agent:Mozilla/5.0(Windows NT 6.1; WOW64; rv:11.0)Gecko/20100101 Firefox/11.0
接受:text/html,application/xhtml + xml, application/xml; q = 0.9,/ ; q = 0.8
Accept-Language:fr,fr-fr; q = 0.8,en-us; q = 0.5,en; q = 0.3
Accept-Encoding:gzip,deflate
Connection:keep -alive
Origin:null
Access-Control-Request-Method:POST
Access-Control-Request-Headers:my-first-header,my-second-header
Pragma:no-cache
Cache-Control:no-cache
为什么我的自定义标题会转到Access-Control-Request-Headers:
Access-Control-Request-Headers:my-first-header,my-second-header
我期待像这样的标头值:
My-First-Header:第一个值
My-Second-Header:第二个值
可能吗?
两者有什么区别?
所以我知道这array.size()是一个功能而array.length属性.是否有用于使用其中一个的用例?是一个更有效(我会想象.length明显更快,因为它是一个属性而不是方法调用?)为什么一个人会使用较慢的选项?是否有某些浏览器与其中一个不兼容?
var x = [];
console.log(x.size());
console.log(x.length);
console.log(x.size()==x.length);
x =[1,2,3];
console.log(x.size());
console.log(x.length);
console.log(x.size()==x.length);
Run Code Online (Sandbox Code Playgroud)
将打印:
0, 0, true
3, 3, true
Run Code Online (Sandbox Code Playgroud) 我有一个Array(var cars = [2,3,..]),它包含一些整数.我已经为数组添加了一些值,但我现在需要通过jQuery的.get方法将这个数组发送到一个页面.如何将其转换为JSON对象以进行发送?
我想要做的是计算当前页面中具有相同类的所有元素,然后我将使用它添加到输入表单的名称.基本上我允许用户点击a <span>然后通过这样做添加另一个更多相同类型的项目.但我想不出用jQuery/JavaScript简单计算所有这些的方法.
我打算将项目命名为类似的东西name="whatever(total+1)",如果有人有一个简单的方法来做到这一点,我将非常感激,因为JavaScript不完全是我的母语.
我有一个输入文本是这样的:
<div class="editor-label">
@Html.LabelFor(model => model.EmployeeId, "Employee Number")
</div>
<div class="editor-field textBoxEmployeeNumber">
@Html.EditorFor(model => model.EmployeeId)
@Html.ValidationMessageFor(model => model.EmployeeId)
</div>
Run Code Online (Sandbox Code Playgroud)
我想使用jquery设置此输入文本的值,所以我这样做:
<div class="editor-label">
<label for="EmployeeId">Employee Number</label>
</div>
<div class="editor-field textBoxEmployeeNumber">
<input class="text-box single-line" data-val="true" data-val-number="The field EmployeeId must be a number." data-val-required="The EmployeeId field is required." id="EmployeeId" name="EmployeeId" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="EmployeeId" data-valmsg-replace="true"></span>
</div>Run Code Online (Sandbox Code Playgroud)
但是,它不起作用...我的语法错误是什么?
我有一个问题:我想通过JavaScript重定向到上面的目录.我的代码:
location.href = (location.href).substr(0, (location.href).lastIndexOf('folder'));
Run Code Online (Sandbox Code Playgroud)
网址如下所示:
example.com/path/folder/index.php?file=abc&test=123&lol=cool
重定向会影响到这个:
example.com/path/&test=123&lol=cool
但想拥有这个:
example.com/path/
我怎么能这样做?
我有一个带有一些图像的常规HTML页面(只是常规的<img />HTML标签).我想得到他们的内容,优选base64编码,而不需要重新下载图像(即它已经被浏览器加载,所以现在我想要内容).
我很想用Greasemonkey和Firefox实现这一目标.
我有一个带有文本框的Web表单.如何默认将焦点设置到文本框?
这样的事情:
<body onload='setFocusToTextBox()'>
Run Code Online (Sandbox Code Playgroud)
所以有人可以帮我吗?我不知道如何使用JavaScript将焦点设置到文本框.
<script>
function setFocusToTextBox(){
//What to do here
}
</script>
Run Code Online (Sandbox Code Playgroud) 有谁知道如何使用JavaScript或jQuery添加或创建自定义HTTP标头?
<div id="selected">
<ul>
<li>29</li>
<li>16</li>
<li>5</li>
<li>8</li>
<li>10</li>
<li>7</li>
</ul>
</div>Run Code Online (Sandbox Code Playgroud)
我想要计算<li>元素的总数<div id="selected"></div>.怎么可能使用jQuery .children([selector])?
javascript ×10
jquery ×6
ajax ×2
arrays ×2
html ×2
http-headers ×2
base64 ×1
css ×1
dom ×1
element ×1
firefox ×1
focus ×1
greasemonkey ×1
httprequest ×1
image ×1
json ×1
post ×1
redirect ×1
textbox ×1
url ×1