我正在尝试使用JQuery在Ajax中添加标头请求.
以下是代码: -
$.ajax({
type: "POST",
contentType: "application/json",
url: "http://localhost:8080/core-service/services/v1.0/patients/registerPatients",
data: JSON.stringify(patientDTO),
//crossDomain : true,
dataType: 'json',
headers: {"X-AUTH-TOKEN" : tokken},
success: function(patientDTO) {
console.log("SUCCESS: ", patientDTO);
/* location.href = "fieldagentHRA.html";*/
if (typeof(Storage) !== "undefined") {
localStorage.setItem("patUrn", patientDTO.data);
location.href="fieldagentHRA.html";
}
},
error: function(e) {
console.log("ERROR: ", e);
display(e);
},
done: function(e) {
enableRegisterButton(true);
}
});
然后我使用了Requestly(Requestly是chrome + firefox插件,我们可以手动为请求添加标头).
手动添加标题后: -
在两个图片请求标题中,x-auth-token存在于"ACCESS-CONTROL-REQUEST-HEADERS"中,但是"X-AUTH-TOKEN"标题连同标题值存在于第二pic中,而第二pic中不存在第一pic.
所以我的问题是如何使用JQuery在Ajax中添加请求头?
我需要从mvc控制器获取一个列表来查看使用jquery ajax.我怎样才能做到这一点.这是我的代码.它的警报错误消息.
public class FoodController : Controller
{
[System.Web.Mvc.HttpPost]
public IList<Food> getFoodDetails(int userId)
{
IList<Food> FoodList = new List<Food>();
FoodList = FoodService.getFoodDetails(userId);
return (FoodList);
}
}
Run Code Online (Sandbox Code Playgroud)
function GetFoodDetails() {
debugger;
$.ajax({
type: "POST",
url: "Food/getFoodDetails",
data: '{userId:"' + Id + '"}',
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (result) {
debugger;
alert(result)
},
error: function (response) {
debugger;
alert('eror');
}
});
}
Run Code Online (Sandbox Code Playgroud)

我有一个要求,我需要从客户端的文件夹中检索所有文件名.
因此,我试图使用Jquery检索文件夹中的文件名称,引用此答案.
我的代码如下:
<script>
var fileExt = ".xml";
$(document).ready(
function(){
$.ajax({
//This will retrieve the contents of the folder if the folder is configured as 'browsable'
url: 'xml/',
success: function (data) {
$("#fileNames").html('<ul>');
//List all xml file names in the page
$(data).find('a:contains(" + fileExt + ")').each(function () {
var filename = this.href.replace(window.location, "").replace("http:///", "");
$("#fileNames").append( '<li>'+filename+'</li>');
});
$("#fileNames").append('</ul>');
}
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
HTML代码如下:
<div id="fileNames"></div>
Run Code Online (Sandbox Code Playgroud)
但是当我在chrome和firefox中运行代码时出现以下错误:
chrome:XMLHttpRequest无法加载file:/// E:/ Test/xml /.收到无效回复.因此不允许原点'null'访问.
Firefox:ReferenceError:$未定义
我试过google很多但错误没有解决.
非常感谢您的帮助.
我在ASP.Net Web应用程序中使用WebApi.我在控制器中有一个方法,Delete我想通过使用jQuery的AJAX方法来访问这个方法.以下是我的代码:
[Authorize]
public int Delete(int proposalId)
{
// logic here...
}
Run Code Online (Sandbox Code Playgroud)
$.ajax({
url: "/Controller/Proposal/" + proposalId,
type: "Post",
contentType: "application/json",
success: function() {
bootbox.alert("Proposal deleted successfully.");
ReloadGrid();
},
error: function() {
}
});
Run Code Online (Sandbox Code Playgroud)
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "controller/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"></modules>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
问题是,当我使用POST它时,正在执行另一个以Post开头的方法.有人可以帮我这个吗?
我在部分剃刀视图上使用 $ajax jquery 函数来获取另一个部分视图以及从控制器到页面的强类型模型数据--> 在特定 div 中显示。现在,如果数据模型数据在那里,它可以工作,但如果没有模型数据,我将传递 json 响应,以便我可以检查 razor 视图以避免空异常。我的问题是 $ajax 中的 done 方法没有调用加上 json 响应,我不知道我哪里做错了
$(document).ready(function () {
/*Address*/
$.ajax({
url: '@Url.Action("DisplayStudentAddress")',
type: "GET",
cache: false
}).done(function (data, textStatus, jqXHR) {
alert(data.Response);
$('#studentAddressDisplay').html(data);
}).fail(function (jqXHR, textStatus, errorThrown) {
alert(jqXHR +" "+textStatus+" "+errorThrown);
});
});
Run Code Online (Sandbox Code Playgroud)
[HttpGet]
[Authorize]
public ActionResult DisplayStudentAddress()
{
int _studentEntityID = 0;
_studentEntityID = _studentProfileServices.GetStudentIDByIdentityUserID(User.Identity.GetUserId());
Address _studentAddressModel = new Address();
_studentAddressModel = _studentProfileServices.GetStudentAddressByStudentID(_studentEntityID);
if (_studentAddressModel != null)
{
return PartialView("DisplayStudentAddress_Partial", _studentAddressModel);
} …Run Code Online (Sandbox Code Playgroud) 我正在创建一个程序来使用 JQuery 的 ajax 方法访问本地文件。
该程序在 IE11 及更早版本中正常运行,但在 Microsoft Edge 浏览器中运行失败。
它显示相同的“网络错误”,并转到 Microsoft Edge 中的错误部分。
有人可以让我知道同样的解决方法吗?
这是我的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="js/jquery-1.11.3.min.js"></script>
<script>
$("document").ready(function(){
BrandAllMenu();
});
function BrandAllMenu() {
var xmlPath = "branding.xml";
$.support.cors = true;
$.ajax({
type: "GET",
url: xmlPath,
async: false,
dataType: "xml",
success: function () {
alert("in");
},
error: function (jqXHR, textStatus, errorThrown) {
alert("jqXHR: " + jqXHR.status + " Response text …Run Code Online (Sandbox Code Playgroud) 我试图将数据从webform传递给代码隐藏方法并在webform中获取值,然后打印它.我最初测试以下代码只是简单地发布请求到方法,获取字符串并在页面中打印并且它有效,但在尝试将数据发布回方法时出现问题
$(document).ready(function () {
$(".AddStaffToRoleLink").on("click", function () {
var selectedStaffID = $(this).attr("id");
alert("this is " + selectedStaffID);
$.ajax({
type: "POST",
url: "AddUserInRole.aspx/AddRoleForSelectStaff",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: { selectedStaffID: selectedStaffID },
success: function (response) {
$("#Content").text(response.d);
},
failure: function (response) {
alert(response.d);
}
});
});
});
Run Code Online (Sandbox Code Playgroud)
[WebMethod]
public static string AddRoleForSelectStaff(string selectedStaffID)
{
return "This string is from Code behind " + selectedStaffID;
}
Run Code Online (Sandbox Code Playgroud) jquery-ajaxq ×7
ajax ×3
jquery ×3
asp.net-mvc ×2
javascript ×2
asp.net ×1
asp.net-3.5 ×1
c# ×1
header ×1
webforms ×1