web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

XMLHttpRequest send not working for other browsers

(0) ShareShare
ReportReport
Posted on by

Hi,

I have a basic XMLHttpRequest script that works properly on Internet Explorer:

var req = new XMLHttpRequest();
req.open("POST", encodeURI(oDataURI), false);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Access-Control-Allow-Origin", "*");
req.send();


However, my client has requested that this work for other browsers. For some reason the "req.send()" function does not work for Firefox and Chrome. Has anyone experienced this issue/can provide me suggestions?

Thanks in advance.

*This post is locked for comments

I have the same question (0)
  • Royal King Profile Picture
    27,686 on at

    Post your whole code that will help to point the issue.

  • Suggested answer
    Nick Plourde Profile Picture
    on at

    Here is a sample of code I used recently in CRM 2013. Based upon the SDK sample code and tested in IE8+ and Chrome.

    $.ajax({
       type: "POST",
       url: ClientUrl + "/XRMServices/2011/OrganizationData.svc/" + self._entityType + "Set(guid'" + item[self._entityType + "Id"] + "')",
       data: window.JSON.stringify(item),
       dataType: "json",
       contentType: "application/json; charset=utf-8",
        beforeSend: function (request) {
          request.setRequestHeader("Accept", "application/json");
          request.setRequestHeader("X-HTTP-Method", "MERGE");
        }
    }).done(function (response, a, b, c) {
       // return if sucessful
    }).fail(function (jqXHR, textStatus, errorThrown) {
       alert(jqXHR.responseJSON.error.message.value);
    });
  • Community Member Profile Picture
    on at

    Here's the entire code, it is just a function that returns "req" object. I will parse it later, but I just want to make "req.send" work. Please note that oDataURI is a non-CRM URL, and I know that the URL is written correctly because this 3rd party application has a successful status.

    function SendRequestToABA(oDataURI) {
    
        var req = new XMLHttpRequest();
        req.open("POST", encodeURI(oDataURI), false);
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.setRequestHeader("Access-Control-Allow-Origin", "*");
        req.send();
    
        if (req.readyState == 4 && req.status == 200) {
            req.onreadystatechange = null; //avoids memory leaks
            return req;
        }
        else {
            Xrm.Page.ui.setFormNotification("Error Occured, please check your browser settings or contact your solution administrator.", "ERROR");
            return;
        }
    }


  • Royal King Profile Picture
    27,686 on at

    Use the below tool to generate ODATA code and use generated code in your web resource file. Code generated from this tool works across all the browsers.

    crmrestbuilder.codeplex.com

  • Community Member Profile Picture
    on at

    The error message is very general:

    Error: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'https://www.*******.com/api/..."

    Do I need to add something else to the request header for https requests?

  • Suggested answer
    MilindP Profile Picture
    1,019 on at

    Remove line

    "req.setRequestHeader("Access-Control-Allow-Origin", "*");

    from the code and then try it.

  • Suggested answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    you wrote "Please note that oDataURI is a non-CRM URL, and I know that the URL is written correctly because this 3rd party application has a successful status."

    and the code is working on IE but not on Chrome or Firefox

    This looks like to me a CORS request, and probably it fails on Chrome or Firefox because the call you made it's synchronous (false parameter of this line)

    req.open("POST", encodeURI(oDataURI), false);

    so you can try to make the request asynchronous, and of coruse you need to change the handling of the request, because you didn't implement the onreadystatechange in your code.

  • Community Member Profile Picture
    on at

    Hi thanks for the feedback.

    Milind, I tried removing that line but I get the same error.

    Guido, I don't want this to be asynchronous because what this does is it returns a value that gets passed to the contact form, and then finally saved in CRM.

  • Royal King Profile Picture
    27,686 on at

    If you want to retrieve something OUT OF THE CRM  then it should be GET request but you are using POST.

    Use below tool to generate your script that will work across all the browsers. even you can test script before using it.

    http://crmrestbuilder.codeplex.com/

  • Community Member Profile Picture
    on at

    Hi Chitrarasan,

    I tried to change from "POST" to "GET" but I still get the error. I used the CRMrestbuilder, but it generates the same code as I did already.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans