This time I will bring you jQuery.ajaxWebService to request WebMethod to process Ajax, and jQuery.ajaxWebService to request WebMethod to process Ajax. What are the precautions?The following is a practical case, let's take a look.
To develop ajax programs under WebForm, you need to use general processing programs (*.ashx) or web services (*.asmx), and each ajax request must create such a file. In this way , if there are too many ajax programs in a project, a bunch of .ashx or .asmx will inevitably be generated. Although it is not harmful to the program itself, the bunch of files always looks harmful. So can we throw away these .ashx and .asmx and choose a more concise way to do the ajax program?
The answer is yes, that is: WebMethod. (This method was debugged under .net3.5 version and has problems under .net2.0)
First create a public static method in the aspx.cs file, and then add the WebMethod attribute.
[WebMethod] public static string GetUserName() { //...... }
If you want to operate the session in this method, you must set the EnableSession attribute of WebMethod to true. That is:
[WebMethod(EnableSession = true)]//或[WebMethod(true)] public static string GetUserName() { //...... }
Then we will write an ajax program to access this program, let's use jQuery.
$.ajax({ type: "POST", contentType: "application/json", url: "WebForm2.aspx/GetUserName", data: "{}", dataType: "json", success: function(){.......} });
Here is a brief explanation of several parameters,
type: the type of request, post must be used here. The WebMethod method only accepts post type requests.
ContentType: Content encoding type when sending information to the server. We must use application/json here.
url: The path to the requested server-side handler, in the format of "file name (including suffix)/method name"
data: parameter list. Note that the parameters here must be string in json format, remember to be in string format, such as: "{aa:11,bb:22,cc:33, ...}". If
what you write is not a string, then jquery will serialize it into a string, then what is received on the server side is not in json format, and cannot be empty. Even if there are no parameters, it must be written as " {}", as in the above example.
Many people fail, and this is the reason.
DataType: Data type returned by the server. It must be json, anything else is invalid. Because the webservice returns data in json format, its form is: {"d":"...."}.
Success: Callback function after the request is successful. You can do whatever you want with the returned data here.
We can see that some of the parameter values are fixed, so from the perspective of reusability, we can make an extension for jquery and make a simple encapsulation of the above function:
We create a script file called jquery.extend.js. Write a method called ajaxWebService inside (because webmethod is actually WebService, so this method is also valid for requesting *.asmx). The code is as follows:
/// <summary> /// jQuery原型扩展,重新封装Ajax请求WebServeice /// </summary> /// <param> /// 处理请求的地址 /// /// <param> /// 参数,json格式的字符串 /// /// <param> /// 请求成功后的回调函数 /// $.ajaxWebService = function(url, dataMap, fnSuccess) { $.ajax({ type: "POST", contentType: "application/json", url: url, data: dataMap, dataType: "json", success: fnSuccess }); }
Okay, so we can write the webmethod method like this :
$.ajaxWebService("WebForm2.aspx/GetUserName", "{}", function(result) {......});
Finally: If there are a lot of ajax programs in your project (this situation is possible, I have made a website, it is an SNS, the whole process is ajax, almost every operation uses ajax) , you feel that writing WebMethod methods in various aspx pages is very fragmented, then you can create a special page (such as: WebMethods.aspx) to store them.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to set up an Ajax request to open a new window immediately after a successful request
Ajax request response to open a new window is blocked How to handle interception
The above is the detailed content of jQuery.ajaxWebService requests WebMethod to handle Ajax. For more information, please follow other related articles on the PHP Chinese website!

作为一种基于MVC模式的PHP框架,CakePHP已成为许多Web开发人员的首选。它的结构简单,易于扩展,而其中的AJAX技术更是让开发变得更加高效。在本文中,将介绍如何使用CakePHP中的AJAX。什么是AJAX?在介绍如何在CakePHP中使用AJAX之前,我们先来了解一下什么是AJAX。AJAX是“异步JavaScript和XML”的缩写,是指一种在

Scrapy是一个开源的Python爬虫框架,它可以快速高效地从网站上获取数据。然而,很多网站采用了Ajax异步加载技术,使得Scrapy无法直接获取数据。本文将介绍基于Ajax异步加载的Scrapy实现方法。一、Ajax异步加载原理Ajax异步加载:在传统的页面加载方式中,浏览器发送请求到服务器后,必须等待服务器返回响应并将页面全部加载完毕才能进行下一步操

jquery ajax报错403是因为前端和服务器的域名不同而触发了防盗链机制,其解决办法:1、打开相应的代码文件;2、通过“public CorsFilter corsFilter() {...}”方法设置允许的域即可。

404页面基础配置404错误是www网站访问容易出现的错误。最常见的出错提示:404notfound。404错误页的设置对网站seo有很大的影响,而设置不当,比如直接转跳主页等,会被搜索引擎降权拔毛。404页面的目的应该是告诉用户:你所请求的页面是不存在的,同时引导用户浏览网站其他页面而不是关掉窗口离去。搜索引擎通过http状态码来识别网页的状态。当搜索引擎获得了一个错误链接时,网站应该返回404状态码,告诉搜索引擎放弃对该链接的索引。而如果返回200或302状态码,搜索引擎就会为该链接建立索引

ajax传递中文乱码的解决办法:1、设置统一的编码方式;2、服务器端编码;3、客户端解码;4、设置HTTP响应头;5、使用JSON格式。详细介绍:1、设置统一的编码方式,确保服务器端和客户端使用相同的编码方式,通常情况下,UTF-8是一种常用的编码方式,因为它可以支持多种语言和字符集;2、服务器端编码,在服务器端,确保将中文数据以正确的编码方式进行编码,再传递给客户端等等。

ajax重构指的是在不改变软件现有功能的基础上,通过调整程序代码改善软件的质量、性能,使其程序的设计模式和架构更合理,提高软件的扩展性和维护性;Ajax的实现主要依赖于XMLHttpRequest对象,由于该对象的实例在处理事件完成后就会被销毁,所以在需要调用它的时候就要重新构建。

CSRF代表跨站请求伪造。CSRF是未经授权的用户冒充授权执行的恶意活动。Laravel通过为每个活动用户会话生成csrf令牌来保护此类恶意活动。令牌存储在用户的会话中。如果会话发生变化,它总是会重新生成,因此每个会话都会验证令牌,以确保授权用户正在执行任何任务。以下是访问csrf_token的示例。生成csrf令牌您可以通过两种方式获取令牌。通过使用$request→session()→token()直接使用csrf_token()方法示例<?phpnamespaceApp\Http\C

当提交表单时,捕获提交过程并尝试运行以下代码片段来上传文件-//File1varmyFile=document.getElementById('fileBox').files[0];varreader=newFileReader();reader.readAsText(file,'UTF-8');reader.onload=myFunc;functionmyFunc(event){ varres


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version
Visual web development tools
