我正在asp.net Webforms上进行开发,并且无法从客户端进行Ajax调用时解决问题。我得到了客户端脚本:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "GET",
url: "Default.aspx/getnewmails",
data: {},
dataType: "html",
success: function (str)
{
$("#div2").html(str);
}
});
});
</script>
和服务器端方法,它们在字符串var in中返回html Default.aspx.cs
:
[WebMethod]
public static string getnewmails()
{
string URI = "https://somewebsite.com/GetEmails.aspx";
WebClient webClient = new WebClient();
System.Collections.Specialized.NameValueCollection formData = new System.Collections.Specialized.NameValueCollection();
try
{
formData["uname"] = "john.g";
formData["upass"] = "12345";
byte[] responseBytes = webClient.UploadValues(URI, "POST", formData);
string Result = Encoding.UTF8.GetString(responseBytes);
return Result;
}
catch
{
return "";
}
}
出于某种原因,进行ajax调用时,我得到的是同一页面的html,Default.aspx
而不是我希望从getnewmails方法获得的Html。此外,当在服务器端方法中放置断点时,它不会得到打。
感谢您的协助
我已经更新了您的代码,它对我有用。
更新的代码:
$.ajax({
type: "POST",
url: "Default.aspx/getnewmails",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
$("#div2").html(data.d);
},
error: function (xhr, ajaxOptions, thrownError) {
$("#div2").html('error: ' + xhr.status + ' ' + thrownError);
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句