Ajax调用Webmethod,跨域

时间:2016-04-22 11:53:35

标签: ajax cross-domain webmethod

我有两个Asp项目。在项目A中的对话框关闭时,我试图使用ajax调用在项目B中调用静态web方法。 它不是调用Webmethod而是调用PageLoad。

任何想法我做错了什么?

的WebMethod

[WebMethod]
    public static string UpdateSession()
    {
        return "Test";

    }

 $(function () {
$('div#DialogDiv').on('dialogclose', function (event) {
    CloseDialog("http://localhost:1330/Application_Default.aspx/UpdateSession");
    return false;
  });
});
function CloseDialog(URL) {
jQuery.support.cors = true;

$.ajax({
    type: "GET",
    url: URL,
    data: '{}',
    contentType: "application/json; charset=utf-8",
    dataType: "jsonp",
    success: function (response) {
      alert("success");
       },
    failure: function (response) {
        alert("Failed to trying to find the method: " + URL );
    }
});
return false;

}

1 个答案:

答案 0 :(得分:0)

尝试使用纯javascript

function CloseDialog(URL) {

    var request = new XMLHttpRequest();
    request.open("GET", URL);
    request.onload = function() {
        if (request.status === 200) {
            alert(request.responseText);
            // to convert to JSON object-> JSON.parse(request.responseText);
      } else {
            alert("Failed to trying to find the method: " + URL );
      }
    };
    request.send();

    return false;
}

使用jQuery我会这样做,你不需要更多。它也应该适用于跨域。

function CloseDialog(URL) {

    $.ajax({
        url: URL,
        success: function (response) {
            jQuery.each(result.list, function(i, val) {
                // iterate the JSON object
                // val.node;
            });
        },
        failure: function (response) {
            alert("Failed to trying to find the method: " + URL );
        }
    });

    return false;
}