0

我正在尝试从网站获取一些数据,首先尝试使用 localhost。但获取请求失败。将其更改为 POST 工作正常。

Error: The Resource cannot be found.

控制器动作:

[HttpGet()]
        public ActionResult GetInformation(string id)
        {
            Uri uri = new Uri("http://localhost:65076/ShowDetails?id=" + id);
            HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);

            request.Method = WebRequestMethods.Http.Get;
            HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            string tmp = reader.ReadToEnd();
            response.Close();

            return Json(new { data = tmp });
        }

JavaScript 函数:

function getInformation() {
    var link = '/Home/GetInformation';
    var id = '11111';
    $.ajax({
        type: 'GET',
        url: link,
        data: { id: id },
        dataType: 'json',
        success: function (result) {
            $.each(result.data, function (item, value) {
                ...
            });
        },
        error: function (result) {
               ...
        }
    });
};
4

1 回答 1

2

由于这是一个 GET,请在查询字符串中传递您的数据:

function getInformation() {
    var link = '/Home/GetInformation?id=11111';
    $.ajax({
        type: 'GET',
        url: link,
        dataType: 'json',
        success: function (result) {
            $.each(result.data, function (item, value) {
                ...
            });
        },
        error: function (result) {
               ...
        }
    });
};
于 2013-08-15T21:34:00.313 回答