[英]Calling ASMX from jQuery
我试图从jQuery调用ASMX方法而没有成功。 以下是我的代码,我不明白自己所缺少的内容。
档案Something.js,
function setQuestion() {
$.ajax({
type: "POST",
data: "{}",
dataType: "json",
url: "http: //localhost/BoATransformation/Survey.asmx/GetSurvey",
contentType: "application/json; charset=utf-8",
success: onSuccess
});
}
function onSuccess(msg) {
$("#questionCxt").append(msg);
}
档案SomethingElse.cs,
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class Survey : System.Web.Services.WebService {
public Survey () {
}
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string GetSurvey() {
return "Question: Who is Snoopy?";
}
}
突出的一件事是您具有UseHttpGet=true
但是在jQuery代码中您正在使用POST。
这也是我创建的称为ASMX页面的测试页面。
[WebMethod]
public Catalog[] GetCatalog()
{
Catalog[] catalog = new Catalog[1];
Catalog cat = new Catalog();
cat.Author = "Jim";
cat.BookName ="His Book";
catalog.SetValue(cat, 0);
return catalog;
}
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
type: "POST",
url: "default.asmx/GetCatalog",
cache: false,
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
success: handleHtml,
error: ajaxFailed
});
});
function handleHtml(data, status) {
for (var count in data.d) {
alert(data.d[count].Author);
alert(data.d[count].BookName);
}
}
function ajaxFailed(xmlRequest) {
alert(xmlRequest.status + ' \n\r ' +
xmlRequest.statusText + '\n\r' +
xmlRequest.responseText);
}
</script>
如果需要的话,必须确保将Json指定为响应格式,并由于安全功能而UseHttpGet
:
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public string GetSurvey() {
return "Question: Who is Snoopy?";
}
我遇到了这个问题,并且遇到了同样的问题。 我通过添加解决了它:
[WebInvoke(Method="POST",ResponseFormat=WebMessageFormat.Json)]
如果要使用POST,请在网络方法属性下方。 即:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class Survey : System.Web.Services.WebService {
public Survey () {
}
[WebMethod]
[WebInvoke(Method="POST",ResponseFormat=WebMessageFormat.Json)]
[ScriptMethod(UseHttpGet = true)]
public string GetSurvey() {
return "Question: Who is Snoopy?";
}
}
这是jQuery调用aspx上的page方法的示例,但它类似于asmx页面。
$.ajax(
{
type: "POST",
url: "NDQA.aspx/ValidateRoleName",
data: '{"roleName":"' + $('[id$=RoleNameTextBox]').val() + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: ValidateSuccess,
error: ValidateError
});
如果需要的话,必须确保将Json指定为响应格式,并由于安全功能而放弃UseHttpGet:
如果您阅读该文章,那么您会发现使用UseHttpGet是安全的,因为ASP.NET具有阻止跨站点脚本攻击媒介的功能。
有很多使用GET的正当理由。
他可以删除data参数并将POST更改为GET以使呼叫正常进行。 假设您需要JSON响应,则还需要添加ResponseFormat = ResponseFormat.Json。
以下步骤解决了我的问题,希望对您有所帮助,
若要使用ASP.NET AJAX从脚本中调用此Web服务,请在asmx服务类上方包含以下行,例如
[System.Web.Script.Services.ScriptService]公共类GetData:System.Web.Services.WebService {
在web.config中的system.web下添加协议,如果无法查看配置,请单击链接
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
我还建议按照Jim Scott的建议删除UseHttpGet。
您可以将以下内容添加到选项中,并检查objXMLHttpRequest以查看更详细的错误响应。
error: function(objXMLHttpRequest, textStatus, errorThrown) {
debugger;
}
如果您尝试使用chrome浏览器,请尝试Internet Explorer对我有用,而且它是关于chrome浏览器的,您必须添加扩展名才能在chrome中工作,但我不知道扩展名
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.