2016-04-22 112 views
0

我有兩個Asp項目。在項目A中的對話框關閉時,我正嘗試使用ajax調用來調用項目B中的靜態webmethod。 而不是調用Webmethod它調用PageLoad。Ajax調用Webmethod,跨域

任何想法我做錯了什麼?

的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; 

}

回答

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; 
} 
+0

謝謝,但結果與我的版本相同,頁面加載仍然被調用。 – Bernie

+0

那麼我猜你的ajax代碼很好!必須是您訪問URL的方式。我不知道如何在這件事上幫助你。抱歉! –