簡體   English   中英

如何發送ajax請求以請求XML響應到asp.net MVC 4控制器?

[英]How to send ajax request to request XML response to asp.net MVC 4 controller?

好的,所以我的名為Home的 asp.net MVC4控制器幾乎靜止不動,所以如果我在Firefox或IE中這樣做

http://localhost:1193/Home/?serialcode=123456789123321

它優雅地顯示了一個頁面,其中包含使用上述序列代碼計算的類似82434234代碼

現在,該控制器支持返回XML數據,經過測試並可以與C#代碼一起使用。 現在,我陷入了最后一步……嗯……發送一個簡單的ajax請求以獲取代碼。 這就是我到目前為止

                var xmlHttp;

                **//Problem 1: not sure what url should be like, given above Firefox
                // example ? Should the word Home , or index be in it?**

                var url = "index?serialcode=123456789123321";                    

                if (window.XMLHttpRequest) {
                    // code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp = new XMLHttpRequest();
                }
                else {
                    // code for IE6, IE5
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xml.dataType = "xml";

                **// Problem 2: this line doesn't do anything below**
                xmlHttp.open("GET", url , true);             

                xmlHttp.send(imei);

任何幫助表示感謝...感謝:)

簡化事情並使用jQuery:

$.ajax({
  url: '@Url.Action("Index", "Home")',
  type: 'GET',
  data: { serialcode: 123456789123321 },
  success: function(responseData) {
    // TODO:
  }
});

使用Url.Action()獲取URL。 通過使用簡單的jQuery.ajax()調用替換上面的代碼來傳遞數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM