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);
});

  

 

angularjs跨域调取webservice的更多相关文章

  1. 浅谈跨域以及WebService对跨域的支持

    跨域问题来源于JavaScript的同源策略,即只有 协议+主机名+端口号 (如存在)相同,则允许相互访问.也就是说JavaScript只能访问和操作自己域下的资源,不能访问和操作其他域下的资源. 在 ...

  2. 浅谈跨域以WebService对跨域的支持

    跨域问题来源于JavaScript的同源策略,即只有 协议+主机名+端口号 (如存在)相同,则允许相互访问.也就是说JavaScript只能访问和操作自己域下的资源,不能访问和操作其他域下的资源. 在 ...

  3. jQuery跨域调用WebService

    jQuery跨域调用WebService举例html: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&qu ...

  4. 跨域以及WebService对跨域的支持

    无耻收藏该博主的成果啦!https://www.cnblogs.com/yangecnu/p/introduce-cross-domain.html 通过域验证访问WebService:https:/ ...

  5. jquery+ajax跨域请求webservice

    最近几天在学习webservice...在学习的时候便想到用ajax的方式去请求webservice.. 一直在测试..如果这个被请求的webservice和自己使用的是同一个端口号.则不用考虑那aj ...

  6. AngularJS跨域请求

    本文主要针对网上各种跨域请求的总结,并加入自己的验证判断,实现工作中遇到的跨域问题.所涉及到的领域很小,仅仅局限于:AngularJS CORS post 并同时需要实现json数据传送给服务器. 首 ...

  7. 跨域调用webservice

    本人第一次在博客园写博客. 最近研究js的跨域调用,举个小例子. ASP.net 中webservice 源代码 /// <summary>    /// Service1 的摘要说明   ...

  8. Angularjs跨域

    一.首先我们要明白跨域的字面概念,读过留过印象之后,下面将会有例子进一步解释 有一篇文章<跨域的理解与实现>描述得很清楚,在这里摘录如下: 域(Domain)是Windows网络中独立运行 ...

  9. angularjs跨域post解决方案

    转自:http://www.thinksaas.cn/topics/0/34/34536.html 前端同学李雷和后台同学韩梅梅分别在自己电脑上进行开发,后台接口写好的时候,李雷改动完就把前端代码上传 ...

随机推荐

  1. Bzoj 2453: 维护队列 && Bzoj 2120: 数颜色 分块,bitset

    2453: 维护队列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 578  Solved: 247[Submit][Status][Discuss] ...

  2. 支持Python 2.7的pylot

    想用pylot测试一下板子上面的嵌入式web server.结果报错. E:\pylot_1.26>run.py -a 2 Traceback (most recent call last): ...

  3. 转载:Java多线程中join方法的理解

    转载自:http://uule.iteye.com/blog/1101994 thread.Join把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程.比如在线程B中调用了线程A ...

  4. Java学习之路(一) —— Java命名规范

    Package 的命名 Package 的名字应该都是由一个小写单词组成. Class 的命名 Class 的名字必须由大写字母开头而其他字母都小写的单词组成 Class 变量的命名 变量的名字必须用 ...

  5. QinQ封装及终结详解

    以下内容摘自正在当当网.京东网.卓越网.互动出版网预售,即将正式发售的<华为交换机学习指南>一书(全书近千页).本书是由华为官方授权,国内第一本,也是唯一一本华为交换机权威学习指南,是华为 ...

  6. Robotium---环境搭建及入门示例

    Robotium是一款基于控件的Android自动化测试框架 环境搭建(window): 安装JDK以及集成Android Sdk的eclipise. 入门: 1,下载Robotium Solo 5. ...

  7. [Javascript + rxjs] Using the map method with Observable

    Like an array, Observable has a map method that allows us to transform a sequence into a new Observa ...

  8. android82 文件下载框架xUtils

    package com.itheima.xutils; import java.io.File; import com.lidroid.xutils.HttpUtils; import com.lid ...

  9. MySQL源码:索引相关的数据结构

    http://www.orczhou.com/index.php/2012/11/mysql-source-code-data-structure-about-index/ 本文将尝试介绍MySQL索 ...

  10. webservice 生成代理类

    webservice的调用方式有两种: 1. 直接在vs ide中通过web引用的方式,将发布于某个位置的web服务引进到工程里面.这种方式基本上会用vs.net的人都会.   2. 通过vs 命令提 ...