user793468
user793468

Reputation: 4966

The resource could not be found HttpGet

I am trying to get some data from a website, trying with localhost first. but the Get request fails. Changing it to POST works fine.

Error: The Resource cannot be found.

Controller Action:

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

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) {
               ...
        }
    });
};

Upvotes: 0

Views: 633

Answers (1)

Andy T
Andy T

Reputation: 9881

Since this is a GET, pass in your data in the querystring:

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) {
               ...
        }
    });
};

Upvotes: 2

Related Questions