2

I am trying to return an array of products using a get request. The response returns XML with a 200 request.

Web Service:

[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public List<product> GetAllProducts()
{
    using (SchulteDesignYourOwnEntities db = new SchulteDesignYourOwnEntities())
    {
        return db.products.ToList();
    }
}

Here is my code:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <title></title>
        <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {

                $.ajax({
                    url: 'http://www.organizeliving.com/designwebservice.asmx/GetAllProducts',
                    dataType: 'json',
                    success: function (result) {
                        alert("Result: " + result.length);
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        console.log("Status: " + xhr.status);
                        console.log("Message: " + thrownError);
                    }
                });


            });
        </script>
    </head>

    <body></body>

</html>
2
  • check whether any of the string properties in returned product list contains < character Commented Dec 5, 2012 at 15:31
  • It does. My problem appears that I cannot return json from my .asmx web service. My response is Soap/ XML. Commented Dec 5, 2012 at 15:32

1 Answer 1

15

You have the dataType as 'json'. jQuery will automatically try to parse JSON from the response. If it cannot, it considers it an error.

XML is not valid JSON (it will really hate the opening <). You can either change the dataType to 'xml' (or nothing) or actually emit pure JSON from the server instead.

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

2 Comments

It succeeds, however, my method says result is undefined. It appears it never parses the data.
It says undefined because result is a JSON object, not a string. Either use $.param(result).length or use your own JSON serialiser.

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.