Estou tentando acessar um serviço retornado pelo meu Web API, mas quando acesso pela função $.getJSON('url') recebo a mensagem: "Failed to load resource: the server responded with a status of 404 (Not Found)".
Web API:
public Response Get(string term)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://itunes.apple.com/search?media=music&term=" + term);
try
{
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
if (response.StatusCode != HttpStatusCode.OK)
throw new Exception(String.Format(
"Server error (HTTP {0}: {1}).",
response.StatusCode,
response.StatusDescription));
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Response));
object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
Response jsonResponse = objResponse as Response;
return jsonResponse;
}
}
catch (WebException ex)
{
WebResponse errorResponse = ex.Response;
using (Stream responseStream = errorResponse.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
String errorText = reader.ReadToEnd();
// log errorText
}
throw;
}
}
Model Album:
[DataContract]
public class Album
{
[DataMember(Name = "artistName")]
public string artistName { get; set; }
[DataMember(Name = "trackName")]
public string trackName { set; get; }
[DataMember(Name = "artworkUrl60")]
public Uri artworkUrl60 { set; get; }
[DataMember(Name = "collectionName")]
public string collectionName { set; get; }
[DataMember(Name = "trackTimeMillis")]
public string trackTimeMillis { set; get; }
public DateTime ReleaseDate{set;get;}
[DataMember(Name = "releaseDate")]
public string releaseDate { set; get; }
[OnSerializing]
void OnSerializing(StreamingContext context)
{
this.releaseDate = this.ReleaseDate.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
this.trackTimeMillis = ItunesController.FormatTrackTimeMillis(this.trackTimeMillis);
}
[OnDeserialized]
void OnDeserializing(StreamingContext context)
{
if (this.releaseDate == null)
{
this.ReleaseDate = default(DateTime);
}
else
{
this.ReleaseDate = DateTime.Parse(this.releaseDate);
}
}
}
Model Response:
[DataContract]
public class Response
{
[DataMember(Name = "results")]
public Album[] albuns { set; get; }
}
javascript na página HTML:
<script type="text/javascript">
function find() {
var search = $('#termId').val();
$.getJSON("api/itunes/" + search,
function (data) {
alert(data);
$.each(data, function (key, val) {
alert(val.artistName);
var image = document.getElementById('artId');
image.src = val.artworkUrl60;
$('#artistName').val(val.artistName);
});
})
.fail(
function (jqXHR, textStatus, err) {
$('#result').text('Erro : ' + err);
});
};
</script>
O que posso estar fazendo de errado?