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. Java中重载和重写的区别

    重载 overloading 1) 方法重载是让类以统一的方式处理不同类型数据的一种手段.多个同名函数同时存在,具有不同的参数个数/类型.重载是一个类中多态性的一种表现. 2) Java的方法重载,就 ...

  2. HW4.30

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  3. python爬虫学习(1)__抓取煎蛋图片

    #coding=utf-8 #python_demo 爬取煎蛋妹子图在本地文件夹 import requests import threading import time import os from ...

  4. ubuntu Server LAmp环境

    1. LAMP 的安装 sudo apt-get install apache2 mysql-server mysql-client php5 php5-gd php5-mysql 由于LAMP大部分 ...

  5. In Java, what is the default location for newly created files?

    If the current directory of the application. If e.g. you create a File by using new FileOutputStream ...

  6. UVA 10194 Football (aka Soccer)

     Problem A: Football (aka Soccer)  The Problem Football the most popular sport in the world (america ...

  7. NSLayoutConstraint-代码实现自己主动布局的函数使用方法说明

    [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelat ...

  8. BBC 生命大设计

    BBC霍金的生命的意义一视频中,有一段图案特别漂亮,于是我就写了一下,具体的繁殖规则是这样的:有存活体和死方格两个部分构成,我分别用 ' * ' 和 ‘  ’ 表示.当一个存活体周围有超过三个存活体时 ...

  9. Nginx vs Apache--reference

    May 14th, 2014 - By Walker Rowe https://anturis.com/blog/nginx-vs-apache/ What is the Nginx web and ...

  10. Android低功耗蓝牙(BLE)开发的一点感受

    最近一段时间,因为产品的需要我做了一个基于低功耗蓝牙设备的Android应用,其中碰到了一些困难,使我深深体会到Android开发的难处:不同品牌,不同型号和不同版本之间的差异使得Android应用适 ...