zoukankan      html  css  js  c++  java
  • angularjs跨域调取webservice

    1、配置

    web.config

    <webServices>
    			<!--必须添加-->
    			<protocols>
    				<add name="HttpGet"/>
    				<add name="HttpPost"/>
    			</protocols>
    		</webServices>
    
    		<httpModules>
    			<add name="JsonpHttpModule" type="MJN.Common.JsonpHttpModule" />
    		</httpModules>
    

    webservice.asmx

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Web.Script.Services;
    using MJN.Common;
    
    namespace MJN
    {
        /// <summary>
        /// WebService1 的摘要说明
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
        [System.Web.Script.Services.ScriptService]
    
        public class WebService1 : System.Web.Services.WebService
        {
            [WebMethod]
            public string getTime()
            {
                ResonseMessage result = new ResonseMessage()
                {
                    state = "1",
                    msg = new Random().Next(1, 10000).ToString()
                };
                return result.ToJson();
            }
    
        }
    }
    

     

    angularjs

    services.factory('httpService', ['$resource', '$http', '$q', '$templateCache',
    		function ($resource, $http, $q, $templateCache) {
    		    return {
    		        setting: function (url, data) {
    		            var deferred = $q.defer();
    		            method = (url.indexOf('http') > -1) ? 'JSONP' : 'POST';
    		            $http({
    		                method: method,
    		                url: url,
    		                cache: $templateCache,
    		                data: data
    		                //headers: { 'Content-Type': 'application/json;charset=UTF-8' }
    		                //headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }
    		            }).success(function (data, status, headers, config) {
    		                deferred.resolve(data, status, headers, config);
    		            }).error(function (data, status, headers, config) {
    		                deferred.reject("network error");
    		            });
    		            return deferred.promise;
    		        }
    		    };
    		} ]);
    

      

    调用:

    httpService.setting('http://10.20.26.19/mjn/WebService1.asmx/getTime?callback=JSON_CALLBACK&format=jsonp&t=' + new Date().getTime()).then(function (data, status, headers, config) {
                console.log(data);
            }, function (reason) {
                console.log(reason);
            });
    

      

     

  • 相关阅读:
    GNU风格 汇编语法总结(转)
    CPSR和SPSR(转)
    C语言调用汇编实现字符串对换
    ubuntu如何跑arm程序
    Shell编程之函数调用
    arm汇编--ubuntu12.04 安装arm-linux交叉编译环境
    linux关于bashrc与profile的区别(转)
    Shell如何传递字符串
    打印指针指向的地址值
    在CentOS 6.4中支持exfat格式的U盘
  • 原文地址:https://www.cnblogs.com/goesby/p/5674120.html
Copyright © 2011-2022 走看看