locked
JSON odata error - Bad Request. Bad syntax or something else? RRS feed

  • Question

  • I can't seem to egt the data I need with our JSON script in our CRM environment.

    The script looks like this:

    function SetMedlemsgrupp() {
        try {
            var utbildning = Xrm.Page.getAttribute('stc_utbildning_id').getValue();
     
            if (utbildning != null) {
                $.ajax({
                    type: "GET",
                    contentType: "application/json; charset=utf-8",
                    datatype: "json",
                    url: '/JUTVECKLING/XRMServices/2011/Organization.svc/stc_utbildningSet(guid\'' + utbildning[0].id + '\')?$select=stc_medlemsgrupp_id',
                    beforeSend: function (XMLHttpRequest) {
                        XMLHttpRequest.setRequestHeader("Accept", "application/json");
                    },
                    success: function (data, textStatus, XmlHttpRequest) {
                        var medlemsgrupp = data.d.stc_medlemsgrupp_id;
     
                        if (medlemsgrupp != null) {
                            var medlemsgruppLookup = Xrm.Page.getAttribute('stc_medlemsgrupp_id');
                            medlemsgruppLookup.setValue([{ id: medlemsgrupp.Id, name: medlemsgrupp.Name, entityType: medlemsgrupp.LogicalName }]);
                        }
                    },
                    error: function (XmlHttpRequest, textStatus, errorThrown) {
                        alert('Odata select failed, status: [' + textStatus + '], error: [' + errorThrown + ']');
                    }
                });
            }
        }
        catch (e) {
            alert(e.toString());
        }
    }

    What I get in response is "Bad Request". I can connect to the same URL with LINQpad so I know its reachable and that the params exists.

    Is the syntax wrong or does anyone have any idea what might be the problem?

    Regards

    Linus


    • Edited by SiriusDreams Tuesday, September 24, 2013 8:04 AM
    Tuesday, September 24, 2013 8:03 AM

All replies

  • Try designing your query using this tool:-

    http://crm2011odatatool.codeplex.com/


    Regards Faisal

    Tuesday, September 24, 2013 8:07 AM
  • A possible cause is case-sensitivity. OData uses the LogicalName of entities and attributes, which may not be all lower-case


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Tuesday, September 24, 2013 4:02 PM
    Moderator
  • Hi Linus,

    Make sure you are passing valid/acceptable values to your parameters. Some times 'Bad Request' is also returned when such a parameter value is passed in the request which is not acceptable by CRM. For example, I had issues with a client type drop down and whenever some one passed an invalid drop down value to this parameter, JSON response was 'Bad Request' instead of exception details. 

    Also, you can use Fiddler to compose JSON request and see what happens. 

    Cheers :-)


    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful" Thanks, Imran Abbasi

    • Proposed as answer by Imran I Abbasi Wednesday, September 25, 2013 6:04 AM
    Wednesday, September 25, 2013 6:04 AM