xml jquery

Do you know why it does not show the result in chrome? But it shows on IE 9?
I am able to see the response data shown in google chrome (F12)


An exact error it shown

XMLHttpRequest cannot load http://clientaccesstest.usa.com/wcf/proquotes/RestService/QuoteService/GetNetPrice(066,335281,[AAS3602I],[1],1,SA7)?_=1428685312396.
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:62937' is therefore not allowed access.


The result should be in xml format from wcf services.

           
        #region GetNetPrice
        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Xml, UriTemplate = "QuoteService/GetNetPrice({CompanyID},{CustomerNo},{Model},{Qty},{OrderDate},{RepNo})")]
        String GetNetPrice(string CompanyID, string CustomerNo, string Model, string Qty, string OrderDate, string RepNo);
        #endregion


<script type="text/javascript">
    function GetNetPrice() {
         var GetNetPriceJSon = "http://clientaccesstest.usa.com/wcf/orders/RestService/QuoteService/GetNetPrice";
         //url: GetNetPriceJSon + encodeURIComponent("(066,335281,[AAS3602I],[1],1,SA7)"),
         //var GetNetPriceJSon = "http://clientaccesstest.usa.com/wcf/orders/RestService/QuoteService/GetNetPrice(066,335281,[AAS3602I],[1],1,SA7)";          
         var companyID = "066";
         var dealerNo = "335281";
         var modelNo = "[AAS3602I]";
         var qty = "[1]";
         var orderDate = "1";
         var repNo = "SA7";
         var allInfo = "(" + companyID + "," + dealerNo + "," + modelNo + "," + qty + "," + orderDate + "," + repNo + ")";

         $(document).ready(function () {
            $.ajax({
                crossDomain: true,
                cache: false,
                type: "GET",
                async: false,
                dataType: "text/xml; charset=utf-8", // result: get this data type from the server
                contentType: "text/plain", //send to the server //text/plain
                url: GetNetPriceJSon + allInfo,
                success: function (data) {
                    alert("Success: Data Below:" + data);
                    resultObj = jQuery.parseJSON(data);                                        
                },
                error: function (xhr, status, error) {
                    //alert(xhr.responseText + " : Error GetNetPriceJSon");
                   
                }
            });
        });
    }
</script>
LVL 1
ITsolutionWizardAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Duy PhamFreelance IT ConsultantCommented:
Firstly, you need to make sure that your WCF REST Service allows cross-site script access, see sample configuration here: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/Q_28653962.html#a40720321.

Then try to set Access-Control-Allow-Origin before sending ajax request as below:
         $(document).ready(function () {
             $.ajax({
                 crossDomain: true,
                 cache: false,
                 type: "GET",
                 async: false,
                 contentType: "text/plain", //send to the server //text/plain
                 url: GetNetPriceJSon + allInfo,
                 success: function (data) {
                     alert("Success: Data Below:" + data);
                     resultObj = jQuery.parseJSON(data);                                        
                 },
                 error: function (xhr, status, error) {
                     //alert(xhr.responseText + " : Error GetNetPriceJSon");
                 },
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
                }
             });
         });

Open in new window


On the other hand, your service method returns data in Xml format, then I don't think resultObj = jQuery.parseJSON(data); will work. Also, you might want to remove dataType: "text/xml; charset=utf-8" from above ajax call to avoid conversion error later.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ITsolutionWizardAuthor Commented:
OK then how can I get the Data from XML format?
0
Duy PhamFreelance IT ConsultantCommented:
You will need to parse the XML yourself (e.g.: using jQuery.parseXML()) or find a generic library to help converting XML to JSON such as http://goessner.net/download/prj/jsonxml/.
0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

ITsolutionWizardAuthor Commented:
Please show me in codes..
0
ITsolutionWizardAuthor Commented:
I have error message 405 (Method Not Allowed) when I try using below codes

 beforeSend: function (xhr) {
                    xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
                }
0
Duy PhamFreelance IT ConsultantCommented:
To solve issue of 405 error, try to add Global.aspx class to your WCF service, and add below code to Global.aspx.cs
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
            if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
            {
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");

                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Access-Control-Allow-Origin, Content-Type, Accept");
                HttpContext.Current.Response.End();
            }
        }

Open in new window

Note that for security reason, in production you might want to change Access-Control-Allow-Origin from * to only the trusted domains you want to allow cross-site script access.
0
ITsolutionWizardAuthor Commented:
thank you. I may be able to get it working.

I have one more if you have time.
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_28655061.html
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WCF

From novice to tech pro — start learning today.