zoukankan      html  css  js  c++  java
  • javascript的跨域调用

    【服务端】

    [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.None)]
        [ToolboxItem(false)]
        [System.Web.Script.Services.ScriptService]
        public class Service1 : System.Web.Services.WebService
        {
            [WebMethod]
            public void HelloWorld(string jsonStudent)
            {
                HttpResponse resp = Context.Response;
                string callBack = Context.Request["callback"].ToString();
                resp.Clear();
                resp.ContentEncoding = new UTF8Encoding(false);
                resp.ContentType = "application/json";
                resp.Charset = "utf-8";
                Student stu = JsonSerializer<Student>.Deserialize(jsonStudent);
                resp.Write(callBack+"("+JsonSerializer<Student>.Serialize(stu)+")");
                resp.End();
            }
    
            [WebMethod]
            public void Hello(string js)
            {
                HttpResponse resp = Context.Response;
                resp.Clear();
                resp.Charset = "utf-8";
                string callBack = Context.Request["callback"].ToString();
                resp.ContentEncoding = new UTF8Encoding(false);
                resp.ContentType = "application/json";
                string callBackFun = callBack + "('"+ js +"')";
                resp.Write(callBackFun);
                resp.End();
            }
        }

    【客户端】

    一、完整写法:

    $.ajax(
          {
            url:"http://localhost:3997/Service1.asmx/HelloWorld",
            data:"jsonStudent={"id":"1","name":"董玮"}",   //最好按照标准写法
            contentType:"application/json",
            type:"post",
            dataType:"jsonp",
            success:function(data)
            {
             alert(data.id+"<===>"+data.name);
            }
          });

    二、简化写法:

    $.getJSON("http://localhost:3997/Service1.asmx/Hello?callback=?",{"js":"董玮"},
          function(data)
          {
          alert(data);
          },"post");
  • 相关阅读:
    linux 终端分屏命令vsp(转)
    ACE消息队列(转)
    iovec结构体定义及使用 (转)
    转: 写给想成为前端工程师的同学们 (from 360前端团队)
    转:苹果企业级开发者账号申请流程
    奇舞团的博客(360前端团队)
    腾讯开源组件
    转:android studio入门合集
    Raid分类说明 (from mongodb权威指南)
    转: linux下的自动对时
  • 原文地址:https://www.cnblogs.com/ServiceboyNew/p/4286719.html
Copyright © 2011-2022 走看看