Calling ASMX from jQuery

流过昼夜 提交于 2019-12-17 04:00:09

问题


I am trying to call an ASMX method from jQuery without success. Following is my code, and I don't understand what I am missing.

File 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);
}

File 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?";
    }
}

回答1:


One thing that stands out is you have UseHttpGet=true but in your jQuery code you are using POST.

Also here is a test page I created calling an ASMX page.

[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>



回答2:


You have to make sure you specify Json as the response format if that is what you want and get rid of UseHttpGet due to security features:

[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public string GetSurvey() {
    return "Question: Who is Snoopy?";
}



回答3:


Here is an example of a jQuery call to a page method on an aspx, but it would be similar to an asmx page.

$.ajax(
    {
        type: "POST",
        url: "NDQA.aspx/ValidateRoleName",
        data: '{"roleName":"' + $('[id$=RoleNameTextBox]').val() + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: ValidateSuccess,
        error: ValidateError

    });



回答4:


I came across this question and had the same issue. I solved it by adding:

[WebInvoke(Method="POST",ResponseFormat=WebMessageFormat.Json)]

Below your web method attribute, if you'd like to use POST. ie:

[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?";
    }
}



回答5:


I would also suggest removing UseHttpGet as Jim Scott suggested.

You can add the following to your options and check the objXMLHttpRequest to see a more detailed error response.

error: function(objXMLHttpRequest, textStatus, errorThrown) {
 debugger;               
}



回答6:


You have to make sure you specify Json as the response format if that is what you want and get rid of UseHttpGet due to security features:

If you read that article then you would see that it is safe to use UseHttpGet as ASP.NET has features to block the cross site scripting attack vector.

There are plenty of valid reasons to use GET.

He can remove the data parameter and change POST to GET to make the call work. Assuming you want a JSON response it would be required to add ResponseFormat=ResponseFormat.Json as well.




回答7:


The following Steps solved my problem, hope it helps some one,

  1. To allow this Web Service to be called from script, using ASP.NET AJAX, include the following line above your asmx service class for example

    [System.Web.Script.Services.ScriptService] public class GetData : System.Web.Services.WebService {

  2. Add protocols under system.web in web.config, please click on the link if you are not able to view configuration

https://pastebin.com/CbhjsXZj

<system.web>
<webServices>
  <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost"/>
  </protocols>
</webServices>




回答8:


If you try chrome browser, try internet explorer it worked for me and also it is about chrome browser you must add extension to works in chrome but i dont know the name of extension



来源:https://stackoverflow.com/questions/879362/calling-asmx-from-jquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!