使用jQuery jsonp跨域调用远程ASP.NET Web服务 [英] cross domain call with jQuery jsonp to remote ASP.NET web service
问题描述
这是我的代码..可以任何人请解决这个问题..我从早上挣扎..下面的代码不工作。实际上,当我们发送jasondata作为输入时,我应该得到数据..但我得到错误。它是一个公共网站服务网站。
< html xmlns = http://www.w3.org/1999/xhtml >
< head runat = server >
< title < span class =code-keyword>> < / title >
< script src = Scripts / jquery-1.7.min.js type = text / javascript > < / script >
< script type = text / javascript >
fun ction myQuerySuggestions(){
var searchText = document.getElementById('txtsearch')。value;
searchText ='+ searchText +';
var jasonData ={+'CountryName:'+ searchText +};
$ .ajax({
url:http://www.webservicex.net/globalweather.asmx/GetCitiesByCountry?callback=?,
dataType:jsonp,
crossDomain:true,
jsonpCallback:'blah',
data:jasonData,
contentType:application / json; charset = utf-8,
success:function(data ,status){
alert(status +status);
},
error:function(){alert(error);}
});
}
函数blah(数据){
var result =(typeof data.d)=='string'? eval('('+ data.d +')'):data.d;
$('#summary')。html('< p > 所有新内容。< em > 您打赌!< / em > < / p > 跨度>');
$('#summary')。html(result);
}
< / script >
< / head >
< body >
< 表格 < span class =code-attribute> id = form1 runat = server >
< div >
< label for = txtsearch >
输入国家:
< / label >
< 输入 type = text < span class =code-attribute> id = txtsearch size = 43 style = font-size:12px; font-weight:bold; / >
< br / >
< asp:按钮 ID = btn runat = server 文字 = 提交 OnClientClick = myQuerySuggestions(); return false; / >
< div id = 摘要 runat = 服务器 > < span class =code-keyword>< / div >
< / div >
< / form >
< / body >
< / html >
.ajax({
url:http://www.webservicex.net /globalweather.asmx/GetCitiesByCountry?callback=?,
dataType:jsonp,
crossDomain:true,
jsonpCallback:'blah',
data:jasonData,
contentType:application / json; charset = utf-8,
成功:功能(数据,状态){
alert(状态+状态);
},
错误:function(){alert( 错误);}
});
}
函数blah(数据){
var result =(typeof data.d)=='string' ?eval('('+ data.d +')'):data.d;
('#summary')。html('< p > 所有新内容。< em > 您打赌!< / em > < / p > ');
Here is My code ..could anyone please solve this problem..I was struggling from morning..below code is not working.Actually i should get the data when we send jasondata as input..but i am getting error.its a public web-service site. 这篇关于使用jQuery jsonp跨域调用远程ASP.NET Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
< blockquote>('#summary')。html(result);
}
< / script >
< / head >
< body >
< 表格 id = form1 runat = server >
< div < span class =code-keyword>>
< label = txtsearch >
输入国家/地区:
< / label >
< 输入 type = text id = txtsearch size = 43 style = font-size:12px; font-weight:bold; / >
< br / >
< asp:按钮 ID = btn runat = server 文字 = 提交 OnClientClick = myQuerySuggestions(); return false; / >
< div id = 摘要 runat = 服务器 > < span class =code-keyword>< / div >
< / div >
< / form >
< / body >
< / html > 跨度>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.7.min.js" type="text/javascript"></script>
<script type="text/javascript">
function myQuerySuggestions() {
var searchText = document.getElementById('txtsearch').value;
searchText = "'" + searchText + "'";
var jasonData = "{" + 'CountryName:' + searchText + "}";
$.ajax({
url: "http://www.webservicex.net/globalweather.asmx/GetCitiesByCountry?callback=?",
dataType: "jsonp",
crossDomain: true,
jsonpCallback: 'blah',
data:jasonData,
contentType: "application/json; charset=utf-8",
success: function (data, status) {
alert(status+" status");
},
error: function () { alert("error"); }
});
}
function blah(data) {
var result = (typeof data.d) == 'string' ? eval('(' + data.d + ')') : data.d;
$('#summary').html('<p>All new content. <em>You bet!</em></p>');
$('#summary').html(result);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<label for="txtsearch">
Enter country:
</label>
<input type="text" id="txtsearch" size="43" style="font-size: 12px; font-weight: bold;" />
<br />
<asp:Button ID="btn" runat="server" Text="Submit" OnClientClick="myQuerySuggestions();return false;" />
<div id="summary" runat="server"></div>
</div>
</form>
</body>
</html>
.ajax({
url: "http://www.webservicex.net/globalweather.asmx/GetCitiesByCountry?callback=?",
dataType: "jsonp",
crossDomain: true,
jsonpCallback: 'blah',
data:jasonData,
contentType: "application/json; charset=utf-8",
success: function (data, status) {
alert(status+" status");
},
error: function () { alert("error"); }
});
}
function blah(data) {
var result = (typeof data.d) == 'string' ? eval('(' + data.d + ')') : data.d;
('#summary').html('<p>All new content. <em>You bet!</em></p>');
('#summary').html(result);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<label for="txtsearch">
Enter country:
</label>
<input type="text" id="txtsearch" size="43" style="font-size: 12px; font-weight: bold;" />
<br />
<asp:Button ID="btn" runat="server" Text="Submit" OnClientClick="myQuerySuggestions();return false;" />
<div id="summary" runat="server"></div>
</div>
</form>
</body>
</html>