简单介绍AJAX的实现

AJAX简介

AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML)。

AJAX 不是新的编程语言,而是一种使用现有标准的新方法。

AJAX 最大的优点是在不重新加载整个页面的情况下,可以与服务器交换数据并更新部分网页内容。

AJAX 不需要任何浏览器插件,但需要用户允许JavaScript在浏览器上执行。

前台页面

<script>
  
    $.ajax({
        type: 'POST',
        url: './WebService1.asmx/HelloWorld',
        data: "",
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        timeout: 300000,
        success: function (rtn) {
           alert("成功");
        },
        error: function (xhr, type) {
            alert("失败");
        }
    })
</script>

后台接口


  [WebService(Namespace = "http://tempuri.org/")]
  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  [System.ComponentModel.ToolboxItem(false)]
  [System.Web.Script.Services.ScriptService]
  public class WebService1 : System.Web.Services.WebService{
    public string HelloWorld()
        {
            return "{d:'Hello World'}";
        }
    }

web.xml配置

  <system.web>
    <compilation debug="true" targetFramework="4.6.1"/>
    <httpRuntime targetFramework="4.6.1"/>
    <webServices>
      <protocols>
        <add name= "HttpPost"/>
        <add name= "HttpGet"/>
      </protocols>
    </webServices>
  </system.web>

Ajax工作流程

在前台页面上发起Ajax请求,实际上会创建一个XMLHttpRequest对象,然后发起一个request请求,处理结束后,会返回数据。

猜你喜欢

转载自blog.csdn.net/weixin_41707267/article/details/88842224