0

I've this piece of code which is supposed to get a JSON answer from a localhost site. If I browse this URL from Chrome, I get a good response, but if I try to run this code from the HTML page I get an error (via the VS debug).

Any ideas?

$(document).ready(function () {
  alert("hello");
  var request = $.ajax({
    url: "http://localhost:3157/api/Products",
    type: "GET",
    dataType: "json"
  });
  request.done(function(msg) {
    alert("msg is: " + msg);
  });
  request.fail(function (jqXhr, textStatus) {
    alert("Request failed: " + textStatus);
  });
});

Update - added xml output

<ArrayOfProduct xmlns:i="http://www.w3.org/2001/XMLSchema-instance"     xmlns="http://schemas.datacontract.org/2004/07/FirstWebApi.Models">
    <Product>
        <Category>Groceries</Category>
        <Id>1</Id>
        <Name>Tomato Soup</Name>
        <Price>1</Price>
    </Product>
    <Product>
        <Category>Toys</Category>
        <Id>2</Id>
        <Name>Yo-yo</Name>
        <Price>3.75</Price>
    </Product>
    <Product>
        <Category>Hardware</Category>
        <Id>3</Id>
        <Name>Hammer</Name>
        <Price>16.99</Price>
    </Product>
</ArrayOfProduct>
2
  • What error do you get ? Commented Jul 19, 2013 at 11:34
  • I simply get the word "error" back on the failure Commented Jul 19, 2013 at 11:38

3 Answers 3

1

Make sure your respone from url is in json format

Sign up to request clarification or add additional context in comments.

1 Comment

Tried to set the dataType to xml (which is how I see it in the browser) and also to remove it but it doesn't do the trick.
0

In order to specify json as the dataType you must return json from the endpoint otherwise you'll get a parse error since the json parser will be invoked explicitly.

Working Demo

Broken Demo

Notice the only difference is the endpoint (html vs json).

You could also try removing the dataType attribute and let jQuery figure it out automatically.


Since your xml is semantically correct, the issue must be in the headers you are sending back. Make sure the response's content type is 'text/xml'

Live Demo

HTML

<script id='xmlData' type='text/html'>
<ArrayOfProduct xmlns:i="http://www.w3.org/2001/XMLSchema-instance"     xmlns="http://schemas.datacontract.org/2004/07/FirstWebApi.Models">
    <Product>
        <Category>Groceries</Category>
        <Id>1</Id>
        <Name>Tomato Soup</Name>
        <Price>1</Price>
    </Product>
    <Product>
        <Category>Toys</Category>
        <Id>2</Id>
        <Name>Yo-yo</Name>
        <Price>3.75</Price>
    </Product>
    <Product>
        <Category>Hardware</Category>
        <Id>3</Id>
        <Name>Hammer</Name>
        <Price>16.99</Price>
    </Product>
</ArrayOfProduct>
</script>

JS

$.mockjax({
  url: '/testXML/',
  responseTime: 0,
  contentType: 'text/xml',
  responseText: $('#xmlData').text()
});

$(document).ready(function () {
    $.ajax({
        url: "/testXML/",
        type: "GET"
    }).done(function(data, textStatus, jqXHR) {
        alert($(data).find('Product:eq(0) > Category').text());
    }).fail(function (jqXhr, textStatus) {
        alert("Request failed: " + textStatus);
    });
});

3 Comments

Tried to set the dataType to xml (which is how I see it in the browser) and also to remove it but it doesn't do the trick.
Is your XML well formed? Is there only 1 parent node? <parent><child></child><child><subchild></subchild></child></parent> would be valid, but <parent><child></child></parent><parent><child></child></parent> would not. All your xml needs to be wrapped by a single parent node.
You should add the xml response to your question.
0

After trying to ask someone at work I must have done something differently and got an error message of "No Transport". From there I found the reason & solution here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.