• JS跨域ajax访问


    方式1:jsonp解决跨域访问

    需要服务和js配合

    服务

     

    [WebMethod]
            public void HelloWorld2(string name)
            {
                HttpContext.Current.Response.ContentType = "application/json;charset=utf-8";
                string jsonCallBackFunName = string.Empty;
                jsonCallBackFunName = HttpContext.Current.Request.Params["jsoncallback"].ToString();
                HttpContext.Current.Response.Write(jsonCallBackFunName + "({ "Result": "Helloword2" + name + "" })");
            }

    JS调用

     var dataStr = "name=" + $("#birthday").val();
                    $.ajax({
                        type: "post",
                        url: "http://192.168.0.99:8082/WebService1.asmx/HelloWorld",
                        dataType: "jsonp",
                        jsonp: 'jsoncallback',
                        contentType: "application/jsonp;charset=utf-8",
                        data: dataStr,
                        success: function (result) {
                            //返回结果
                            alert(result.Result);
                            $("#name").val(result.Result);
                        },
                        error: function (e) {
                            window.alert(e.status);
                        }
                    });
                });

    方式2:增加配置处理跨域

    如果是在.net下则在web.config中增加配置

    在system.webServer下增加可跨域访问

     <httpProtocol>
          <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*" />
            <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
          </customHeaders>
        </httpProtocol>

    如果是调用webservice在服务端config中增加配置在system.web下增加

    <protocols>
            <add name="HttpSoap"/>
            <add name="HttpPost"/>
            <add name="HttpGet"/>
            <add name="Documentation"/>
    </protocols>

    服务

     [WebMethod]
            public string HelloWorld1(string name)
            {
                return "{data:你好:" + name + "}";
            }

    前台调用方式

    $.ajax({
                        type: "post",
                        url: "http://192.168.0.99:8082/WebService1.asmx/HelloWorld",
                        dataType: "json",
                        data: "{name:'" + birthday + "'}",//参数
                        contentType: "application/json;charset=utf-8",
                        success: function (result) {
                            //返回结果  
                            $("#name").val(result.d);
                        },
                        error: function (e) {
                            window.alert(e.status);
                        }
                    });
                });

    原文路径:http://www.cnblogs.com/yx007/p/8073264.html

  • 相关阅读:
    span设置宽和高当没有内容的时候也可撑开
    span设置宽和高当没有内容的时候也可以撑开
    块级元素以及内联元素大总结
    内存泄露问题
    Sqlcompletefree
    运用SET ANSI_PADDING OFF创建某个字段为自增列的表,以及插入数据
    Sql Server中通配符
    sql server 2008查询窗口怎么显示行数
    sql server 2008语句中的go有什么用?
    SQL Server 2008 R2[ALTER]列属性修改
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/9957474.html
Copyright © 2020-2023  润新知