js用jsonp跨域问题
2016-01-08 14:26
627 查看
提交button如果用<button>标签提示,不允许跨域
改成<input 就好了..
前段:
$("#btnloginsb").click(function () {
var name = $("#firstname").val();
var pwd = $("#lastname").val();
$.ajax({
url: "http://localhost:38620/client/ClientLogin?callback=ff",
type: "get",
cache: false,
jsonp: "",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(一般默认为:callback)
jsonpCallback: "myCallBack",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名,也可以写"?",jQuery会自动为你处理数据
crossDomain: true,
data: { name: name, pwd: pwd },
dataType: 'jsonp',
success: function (html) {
alert("html" + html);
},
error: function (code) {
alert("error" + code);
}
});
});
后端:
if (client != null)
{
//need to use
Session["CurrentCustomer"] = client;
retResult.obj = client.AllName;
retResult.success = true;
}
else
{
retResult.obj = "用户名密码错误";
retResult.success = false;
}
string test = new JavaScriptSerializer().Serialize(retResult);
return new ContentResult()
{
Content = string.Format("myCallBack({0})", test),
ContentType = "application/script"
};
改成<input 就好了..
前段:
$("#btnloginsb").click(function () {
var name = $("#firstname").val();
var pwd = $("#lastname").val();
$.ajax({
url: "http://localhost:38620/client/ClientLogin?callback=ff",
type: "get",
cache: false,
jsonp: "",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(一般默认为:callback)
jsonpCallback: "myCallBack",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名,也可以写"?",jQuery会自动为你处理数据
crossDomain: true,
data: { name: name, pwd: pwd },
dataType: 'jsonp',
success: function (html) {
alert("html" + html);
},
error: function (code) {
alert("error" + code);
}
});
});
后端:
if (client != null)
{
//need to use
Session["CurrentCustomer"] = client;
retResult.obj = client.AllName;
retResult.success = true;
}
else
{
retResult.obj = "用户名密码错误";
retResult.success = false;
}
string test = new JavaScriptSerializer().Serialize(retResult);
return new ContentResult()
{
Content = string.Format("myCallBack({0})", test),
ContentType = "application/script"
};
相关文章推荐
- JSONArray的应用
- js判断当前的访问是手机/电脑
- js判断当前的访问是手机还是电脑
- javascript弹性运动效果简单实现方法
- js判断访问的当前设备是手机还是电脑
- js 中实现aop
- javascript表格内容的展开和折叠
- 读书笔记 JavaScript:The Good Parts
- JavaScript跨域总结与解决办法
- js中 正則表達式
- ExtJS学习--------Ext.Element中的经常使用事件和其它重要的方法学习(实例)
- Javascript addEventListener dispatchEvent
- js日期时间函数
- JsonConvert 使用注意事项之 Serializable
- javascript运动效果实例总结(放大缩小、滑动淡入、滚动)
- json
- jsp页面通过ajax取值/展示数据及分页显示
- c# string.format json字符串 formatException错误
- 2016.01.08 Javascript视频
- js-获取前台标签多选的值