C# jquery webservices 跨域调用的问题解决方案
前台代码:
<script src="js/jquery-1.9.1.min.js" type="text/javascript"></script>
$('#getString').click(function() {
$.ajax({
url: url + "jQueryMobile.asmx/GetProjInfoList?jsoncallback=?", //webservice总返回Json数据的方法
type: 'POST',
data: {UserId:'majunfei'},
//contentType: "application/json",
dataType: "json",
success: function(res) {
//alert(res);
// var strRet = res.d; //Net3.5
alert(res.result);
strRet = $.parseJSON(res); //eval("(" + strRet + ")");
var b_ok = strRet.success;
var strInfo = strRet.info;
alert(strInfo);
$.each(strRet.rows, function(index, row) {
alert(row.proj_code);
alert(row.proj_name)
});
//data = $.parseJSON(''+data+'');//jquery1.4
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
$("div").html(textStatus);
$("div").append("<br/>status:" + XMLHttpRequest.status);
$("div").append("<br/>readyState:" + XMLHttpRequest.readyState);
$("div").append("<br/>responseText:" + XMLHttpRequest.responseText);
}
});
// var clientUrl = "http://172.20.1.71:8099/jQueryMobile.asmx/GetProjInfoList?jsoncallback=?";
// $.getJSON(
// clientUrl,
// { UserId: 'majunfei' },
// function(json) {
// alert(json.result);
// // $("#data").html("城市:" + json.city + ",时间:" + json.dateTime);
// }
// );
// $.ajax({
// url: clientUrl,
// dataType: "jsonp",
// data : {UserId: 'majunfei'},
// success : OnSuccess,
// error : OnError
// });
// function OnSuccess(responseData) {
// alert(responseData.result);
// // $("#data").html(responseData.city);
// }
// function OnError(XMLHttpRequest, textStatus, errorThrown) {
// targetDiv = $("#data");
// if (errorThrown || textStatus == "error" || textStatus == "parsererror" || textStatus == "notmodified") {
// targetDiv.replaceWith("请求数据时发生错误!");
// return;
// }
// if (textStatus == "timeout") {
// targetDiv.replaceWith("请求数据超时!");
// return;
// }
// }
// $.ajax({
// type: "get",
// // data: "{UserId:'majunfei'}",
// async: false,
// url: "http://172.20.1.71:8099/jQueryMobile.asmx/GetProjInfoList",
// dataType: "jsonp",
// jsonp: "callback", //传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(一般默认为:callback)
// jsonpCallback: "flightHandler", //自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名,也可以写"?",jQuery会自动为你处理数据
// success: function(res) {
// var strRet = res.d; //Net3.5
// //alert(strRet);
// strRet = eval("(" + strRet + ")");
// var b_ok = strRet.success;
// var strInfo = strRet.info;
// alert(strInfo);
// $.each(strRet.rows, function(index, row) {
// alert(row.proj_code);
// alert(row.proj_name)
// });
// //data = $.parseJSON(''+data+'');//jquery1.4
// },
// error: function() {
// alert('fail');
// }
// });
});
后台代码:
/// <summary>
///WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{ /// <summary>
/// 根据当前用户帐户,获取项目信息列表
/// </summary>
/// <param name="UserId">当前用户帐户</param>
/// <returns></returns>
[WebMethod]
public void GetProjInfoList(string UserId)
{
// string callback = HttpContext.Current.Request["jsoncallback"];
string callbackMethodName = HttpContext.Current.Request.Params["jsoncallback"] ?? "";
var m_DicRoot = new Dictionary<string, object>();
try
{
string strSql = "select proj_code,proj_name from FN_PM_UserDefaultProjLimits('" + UserId + "') where sortNo<>10";
DataTable dt_projInfo = TmpSqlServer.ExecuteSqlRead(strSql);
m_DicRoot.Add("success", true);
m_DicRoot.Add("info", "读取数据成功");
var list = new List<Dictionary<string, object>>();
foreach (DataRow dr in dt_projInfo.Rows)
{
var m_dic = new Dictionary<string, object>();
m_dic.Add("proj_code", dr["proj_code"].ToString());
m_dic.Add("proj_name", dr["proj_name"].ToString());
list.Add(m_dic);
}
m_DicRoot.Add("rows", list);
}
catch (Exception e)
{
m_DicRoot.Add("success", false);
m_DicRoot.Add("info", e.ToString());
}
//关于result这词是你自己自定义的属性
//会作为回调参数的属性供你调用结果 21
//HttpContext.Current.Response.ContentType = "application/json";
//HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
// HttpContext.Current.Response.Write(callbackMethodName + "({result:'true'})");
HttpContext.Current.Response.Write(callbackMethodName + "({result:'" + ListToJson(m_DicRoot) + "'})");
HttpContext.Current.Response.End(); //HttpContext.Current.Response.Write( ListToJson(m_DicRoot));
//return ListToJson(m_DicRoot);
} } }
C# jquery webservices 跨域调用的问题解决方案的更多相关文章
- jquery Ajax跨域调用WebServices方法
由于公司需要开发一个手机页面,想提供给同事直接在手机上可以查询SAP资料.数据需要使用js调用webserver来获取. 因为初次使用Jquery调用Webserver,所以期间并不顺利.测试调用We ...
- Jquery的跨域调用
JQuery1.2后getJSON方法支持跨域读取json数据,原理是利用一个叫做jsonp的概念.当然,究其本质还是通过script标签动态加载js,似乎这是实现真正跨域的唯一方法. getJSON ...
- jquery ajax跨域调用
客户端: //ajax跨域调用的方法 $.ajax({ url:dustUrl+"/screenshot/getDevices.do", type: "get" ...
- jquery ajax 无法跨域调用的解决办法
今天要用到jquery ajax 跨域调用,但是ajax是禁止跨域调用的,所以只能先在php文件使用函数取得跨域的值,然后用ajax调用本地php文件.
- jquery中的jsonp跨域调用
jquery jsonp跨域调用接口
- jquery中的jsonp跨域调用(接口)
jquery jsonp跨域调用接口
- jquery post跨域请求数据
原先一直以为要实现跨域请求只能用jsonp,只能支持GET请求,后来了解到使用POST请求也可以实现跨域,但是需要在服务器增加Access-Control-Allow-Origin和Access-Co ...
- jQuery跨域调用WebService
jQuery跨域调用WebService举例html: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&qu ...
- 实现jquery.ajax及原生的XMLHttpRequest跨域调用WCF服务的方法
关于ajax跨域调用WCF服务的方法很多,经过我反复的代码测试,认为如下方法是最为简便的,当然也不能说别人的方法是错误的,下面就来上代码,WCF服务定义还是延用上次的,如: namespace Wcf ...
随机推荐
- WPF中ControlTemplate和DataTemplate的区别
下面代码很好的解释了它们之间的区别: <Window x:Class="WPFTestMe.Window12" xmlns="http://schemas.micr ...
- Visual Studio蛋疼问题解决
监视变量显示未定义标识符: VS2012在编译的时候采用了较快的编译模式,所以有些变量就显示未定义了. 解决方案: 项目->属性->c/c++->优化->改为禁用/OD ...
- H5 浏览器开发文档
http://sja.co.uk/controlling-which-ios-keyboard-is-shown https://developer.apple.com/library/safari/ ...
- sql:找出工资第二高的人名
CREATE TABLE EmpSalaryInfo ( Id ), Name ), Salary int ) ) ) ) ) 方法1 (子查询): name from test where sala ...
- jquery dialog open后,服务器端控件失效的快速解决方法
jquery dialog为我们提供了非常漂亮实用的对话框,比单调的alert.confirm.prompt好用很多. 在使用jquery与.net共同开发时,直接调用jquery dialog的op ...
- mapreduce作业状态一直是ACCEPTED
搭建yarn环境后,执行 hadoop/bin/hadoop jar hadoop/share/hadoop/mapreduce/hadoop-mapreduce-examples-2.4.1.jar ...
- 转载:C# this.Invoke()的作用与用法 理解三
Invoke()的作用是:在应用程序的主线程上执行指定的委托.一般应用:在辅助线程中修改UI线程( 主线程 )中对象的属性时,调用this.Invoke(); 在多线程编程中,我们经常要在工作线程 ...
- Java面向对象编程
面向对象的软件开发: 面向对象的开发把软件系统看成各种对象的集合,对象就是最小的子系统,一组相关的对象能够组合成复杂的子系统. 面向对象的开发方法具有以下优点: 1.把软件系统看成是各种对象的集合,更 ...
- 获取客户端真实ip
// 获取客户端真实ip() protected function getIP() { global $ip; if (getenv("HTTP_CLIENT_IP")) $ip ...
- ArcGIS AddIN开发之自定义鼠标样式
如果想修改Windows默认的鼠标样式,可以这样 //设置鼠标样式 this.Cursor = System.Windows.Forms.Cursors.Cross; 可是如果想设置成一些自定义的很好 ...