[英]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.