Tag: ajax

为什么添加额外的标头使AJAX调用失败

AJAX电话: $.ajax({ url: “http://myserver2:296/api/Demo/HelloWorld”, type: “GET”, dataType: ‘JSONP’, jsonp: “callback”, headers: { ‘API_KEY’: ‘mykey09090’ }, success: function (result) { console.log(result); }, error: ajaxFailed }); function ajaxFailed(xmlRequest) { alert(xmlRequest.status + ‘ \n\r ‘ + xmlRequest.statusText + ‘\n\r’ + xmlRequest.responseText); } 我收到以下错误: Failed to load resource: the server responded with a status of 403 (Forbidden) 。 但是当我使用Postman时,我只需要添加带有http://myserver2:296/api/Demo/HelloWorld […]

访问.cs文件中声明的web方法与ajax中的任何aspx或ascx文件无关(jquery)

您好我将一个Web方法从一个aspx页面的代码隐藏文件移动到另一个cs文件,该文件存在于数据部分(不包含任何aspx页面)。 以前我曾经通过使用Ajax访问web方法,类似于url type: “post”, contentType: “application/json; charset=utf-8”, dataType: “json”, url: “Results.aspx/EmployeeSummaryHistory”, // call history function data: JSON.stringify(emp), success: function (resp) { 但现在我正在尝试使用Url访问移动的Web方法 type: “post”, contentType: “application/json; charset=utf-8”, dataType: “json”, url: “~/Model/Data/EmployeeRepository.cs/EmployeeSummaryHistory”, // call history function data: JSON.stringify(emp), success: function (resp) { 但我收到错误,我不知道如何访问.cs文件中声明的Web方法,该文件不包含任何与之关联的aspx文件,请帮助我。 我的网络方法就像 [WebMethod] public static List EmployeeSummaryHistory(string empNo) { var employee = new RefEmployeeRepository(); […]

Ajax.BeginForm导致重定向到部分视图而不是就地

我的Search.cshtml有一个名为“search-results”的div,需要更新。 SearchResults是动作名称。 我在MVC2 / VS2008项目上做了很多次,但这是我第一次使用MVC3和VS2010。 问题是,而不是我的搜索结果在我的div中呈现,它点击提交重定向我显示我的部分作为独立页面。 我已经读过这可能是因为没有启用Ajax。 我的_Layout.cshtml看起来像这样: @ViewBag.Title @(Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => group.Add(“telerik.common.css”).Add(“telerik.transparent.css”).Combined(true).Compress(true))) My MVC Application @(Html.Telerik().Menu() .Name(“menu”) .Items(menu => { menu.Add().Text(“Home”).Action(“Index”, “Home”); menu.Add().Text(“About”).Action(“About”, “Home”); menu.Add().Text(“Employees”).Action(“List”, “Employee”); })) @RenderBody() @(Html.Telerik().ScriptRegistrar().DefaultGroup(group => group.Combined(true).Compress(true))) 我是否需要添加MicrosoftMvcAjax.js或jquery.unobtrusive-ajax.js? 我的web.config(root)包含以下内容: 如果我在元素的末尾添加MicrosoftMvcAjax.js,我得到一个错误,说明命名空间’Type’是来自MicrosoftMvcAjax.js第一行的未定义错误: Type.registerNamespace(‘Sys.Mvc’);Sys.Mvc.$create_AjaxOptions=function(){return {};} 我在这里想念的是什么 我确信我的代码很好,因为它几乎从我的MVC2项目中逐字复制。

jQuery使用AJAX调用传递文件

如果我有一个返回CSV的AJAX调用,如何让浏览器提示用户下载? 下面,ProductsExport将在成功数据中返回CSV。 我只需要用@替换//将文件传递给用户行… $.ajax({ type: “POST”, url: “/Search/ProductsExport”, data: $(‘#CustomerId’).serialize(), success: function (data) { // Deliver file to user!! }, error: function (xhr, textstatus, errorThrown) { alert(‘Error’); } }) 我后面的C#代码如下所示: var aFileContent = Encoding.ASCII.GetBytes(export); var aMemoryStream = new MemoryStream(aFileContent); return File(aMemoryStream, “text/plain”, string.Format(“{0}.csv”, CustomerId));

使用validation器在Asp.netfunction中是否存在任何电子邮件?

我的要求是,如果用户在asp.net文本框中输入电子邮件,它应该从数据库validation,这个电子邮件是否存在,并且应该停止提交表单,是否有任何内置function或我必须自己制作? 谢谢Atif

使用用户控件的延迟加载选项卡

我想在AJAX选项卡容器中使用延迟加载选项卡。 我已经实现了它。 但是我遇到的问题是,当我点击一个按钮或触发该用户控件中的任何事件时,它不会被触发; 什么都没发生。 代码隐藏: protected void TabContainerUp_ActiveTabChanged(object sender, EventArgs e) { string tabName = TabContainerUp.ActiveTab.ID; getActiveTab(tabName); } public void getActiveTab(string tabName) { UserControl uc; //uc. switch (tabName) { case “tab1”: pnlUC1.Controls.Clear(); uc = Page.LoadControl(“~/Controls/UC1.ascx”) as UserControl; pnlUC1.Controls.Add(uc); break; case “tab2”: pnlUC2.Controls.Clear(); uc = Page.LoadControl(“~/Controls/UC1.ascx”) as UserControl; pnlUC2.Controls.Add(uc); break; } }

c#已启用CORS的Web Api和所请求资源上存在可怕的“Access-Control-Allow-Origin”标头

我有一个C#Web API项目。 我使用Web API 2标准创建了一个Controller。 我试图只在一个动作上启用CORS,这就是我所拥有的: namespace MyProject.Controllers { public class MyControllerController : ApiController { // POST api/mycontroller [EnableCors(“http://link.myurl.com”, “*”, “*”)] public bool Post(MyCustomObject customObject) { // Function that returns Bool return myFunction(customObject); } } } 我的WebApiConfig.cs看起来像这样: // Web API configuration and services config.EnableCors(); // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: “DefaultApi”, routeTemplate: “api/{controller}/{id}”, defaults: […]

Request.Files始终为null

我正在编写一个C#ASP.Net MVC应用程序,以便客户端将文件发布到其他服务器。 我正在使用通用处理程序来处理从客户端到服务器的已发布文件。 但是在我的处理程序中,System.Web.HttpContext.Current.Request.Files总是为空(0计数)。 表格代码: @model ITDB102.Models.UploadFileResultsModels @{ Layout = “~/Views/Shared/_Layout.cshtml”; } Upload File Send File @section scripts{ // Variable to store your files var files; var form = document.getElementById(‘file-form’); // Add events $(‘input[type=file]’).on(‘change’, prepareUpload); // Grab the files and set them to our variable function prepareUpload(event) { files = $(‘#FilePath’).get(0).files; } form.onsubmit = function […]

WCF服务返回400 Bad Request

我有这个应用程序在本地和部署时使用.mdf SQL Express数据库文件(我通常用于测试目的)。 但是,当我将其更改为与我们的SQL Server 2008一起使用时,应用程序可以正常工作,但服务却没有。 例如,如果在我的代码页面后面我有一个按钮,将数据添加到这样的表,它可以正常工作: public static string connString = @”Data Source=server1;Initial Catalog=Project;Integrated Security=True”; protected void btnAddProj_Click(object sender, EventArgs e) { using (var sqlc = new SqlConnection(connString)) { sqlc.Open(); var cmd = sqlc.CreateCommand(); int intProjectID; // Add the project info to the database cmd.CommandText = “INSERT INTO tblProject VALUES(@ProjName,@ProjTeam,@ProjStart,@ProjEnd)”; cmd.Parameters.Add(“ProjName”, System.Data.SqlDbType.NVarChar).Value = txtProjName.Text; […]

开发聊天模块

我正在开发asp.net中的在线聊天应用程序。 我面临一个问题,比如假设有两个用户开始通信。 当第一个用户在基于javascript的聊天框中写入消息然后ajax请求转到服务器。 我如何打开第二个用户聊天框,以便他回复第一个用户消息?