Link to home
Start Free TrialLog in
Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America

asked on

wcf, asp.net, jquery /ajax restful

Below code working in IE but not Chrome.  And I verified the service actually went thought to the browser.
I believe something wrong when it is passing the parameters like below

url: GetSaleTax + encodeURIComponent("(066,''," + city + "," + state + "," + zip + ",USA)"),

But I do not know how to fix it. I also have another service call and it is working but that one does not need to pass parameters.

Second Question is: I want pass SalesTaxRate to input text box "taxRate". Please help.

return error:
XMLHttpRequest cannot load http://clientaccesstest.abc.com/wcf/orders/RestService/ProQuoteServiceJVC/GetSaleTax(066%2C''%2CCOLUMBUS%2CIN%2C47201%2CUSA)?_=1428433379116. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:62937' is therefore not allowed access.

export result after service is called:

<Status>Completed</Status><CustomerNumber></CustomerNumber><ShipToCity>COLUMBUS</ShipToCity><ShipToState>IN</ShipToState><ShipToZip>47201</ShipToZip><ShipToCountry>USA</ShipToCountry><SalesTaxRate>7.000</SalesTaxRate>

Script:

<script type="text/javascript">
    function GetSaleTaxList(city, state, zip) {

        city = "COLUMBUS";
        state = "IN";
        zip = "47201";

        var GetSaleTax = "http://clientaccesstest.abc.com/wcf/orders/RestService/ProQuoteServiceJVC/GetSaleTax";            
            if (city == '' || state == '' || zip == '' ) {
                  //setSaleTaxInfo("NO RATE");
            }else if (state == 'XX') {
                  //setSaleTaxInfo("NO RATE");
            }else{
                  $(document).ready(function () {
                              
                        //alert(GetSaleTax);
                        $.ajax({
                              cache: false,
                              type: "GET",
                              async: false,
                              dataType: "json",
                              contentType: 'text/plain',
                              url: GetSaleTax + encodeURIComponent("(066,''," + city + "," + state + "," + zip + ",USA)"),
                              success: function (data) {
                                    $('#data').html(data);
                                    alert(data + " : data");
                                    if (data == null)
                                          return;
                                    //setSaleTaxInfo(data);
                                    //resultObj = jQuery.parseJSON(data);
                                    //if (resultObj.Status != 'Failed') {
                                    //    $('#json').html(setSaleTaxInfo(resultObj.Data));
                                    //}
                              },
                              error: function (xhr) {
                                    //$('#data').html(xhr.responseText);
                                    alert(xhr.responseText + " : error GetSaleTax");

                              }
                        });
                  });
            }
}
</script>

<input type="text" name="taxRate" id="taxRate">
Avatar of ambience
ambience
Flag of Pakistan image

This part looks suspicious

"(066,''," + city

shouldnt it be

"(066,'' + city
Avatar of ITsolutionWizard

ASKER

even I use "(066,'' + city  same issue
I see a couple potential problems.

1. You are registering your jQuery $(document).ready() event within your GetSaleTaxList() function.  There are some unlikely reasons to do this, but I don't think this is your intention here.

2. encodeURIComponent() returns a url encoded string, and it requires one string as a parameter.  You have 5 comma delimited parameters. The first parameter is "(066,", the second parameter is " + city + ", etc...

I think this should be closer to what you are expecting:
<script type="text/javascript">
$(document).ready(function () {
    function GetSaleTaxList(city, state, zip) {

        city = "COLUMBUS";
        state = "IN";
        zip = "47201";

        var GetSaleTax = "http://clientaccesstest.abc.com/wcf/orders/RestService/ProQuoteServiceJVC/GetSaleTax";            
            if (city == '' || state == '' || zip == '' ) {
                  //setSaleTaxInfo("NO RATE");
            }else if (state == 'XX') {
                  //setSaleTaxInfo("NO RATE");
            }else{
                        //alert(GetSaleTax);
                        $.ajax({
                              cache: false,
                              type: "GET",
                              async: false,
                              dataType: "json",
                              contentType: 'text/plain',
                              url: GetSaleTax + encodeURIComponent("(066," + city + "," + state + "," + zip + ",USA)"),
                              success: function (data) {
                                    $('#data').html(data);
                                    alert(data + " : data");
                                    if (data == null)
                                          return;
                                    //setSaleTaxInfo(data);
                                    //resultObj = jQuery.parseJSON(data);
                                    //if (resultObj.Status != 'Failed') {
                                    //    $('#json').html(setSaleTaxInfo(resultObj.Data));
                                    //}
                              },
                              error: function (xhr) {
                                    //$('#data').html(xhr.responseText);
                                    alert(xhr.responseText + " : error GetSaleTax");

                              }
                  });
            }
     }
});
</script>

<input type="text" name="taxRate" id="taxRate">

Open in new window

Let me repost the codes. I may copied something wrong in the past. sorry.
Overall, below is correct codes, and even I use your way to call. It still returns error. See attached.
Also, the screenshot show red on right, and may be that is the cause. not sure.

<script type="text/javascript">
    function GetSaleTaxList() {
        var city = "COLUMBUS";
        var state = "IN";
        var zip = "47201";
        var str = "(066,337557,COLUMBUS,IN,47201,USA)";
        var GetSaleTax = "http://clientaccesstest.abc.com/wcf/orders/RestService/QuoteService/GetSaleTax";
        if (city == '' || state == '' || zip == '') {
            //setSaleTaxInfo("NO RATE");
        } else if (state == 'XX') {
            //setSaleTaxInfo("NO RATE");
        } else {
            $(document).ready(function () {
                //alert(GetSaleTax);
                $.ajax({
                    cache: false,
                    type: "GET",
                    async: false,
                    dataType: "json",
                    contentType: 'text/plain',
                    url: GetSaleTax + encodeURIComponent("(066,337557," + city + "," + state + "," + zip + ",USA)"),
                    processData: true,
                    // [WebGet(UriTemplate = "ProQuoteServiceJVC/GetSaleTax({CompanyID},{CustomerNo},{ShipToCity},{ShipToState},{ShipToZip},{ShipToCountry})")]

                    success: function (data) {
                        alert(data + " : data");
                        if (data == null)
                            return;                        
                    },
                    error: function (xhr) {
                        //$('#data').html(xhr.responseText);
                        alert(xhr.responseText + " : error GetSaleTax");

                    }
                });
            });
        }
    }
</script>
error8.jpg
don't worry about the service name. I re-name for security purpose.
Can you check the output in Firebug and post it here? There must be some errors in the console tab
yes. please view attachment.
error8.jpg
This looks an error in the returned JSON data. Can you also paste the raw returned JSON? Also, the contents of the Console tab?
how can I do that in chrome?
I guess you want raw response. see attached. one from chrome, one from IE.
error8a.jpg
error8b.jpg
I also have another wcf call in jquery and return below result. I copy and paste from Chrome.

And still this call has same error as the one posted.

<Status>Completed</Status><CustomerNumber>335281</CustomerNumber><OrderDate>04-08-15</OrderDate><OrderDetail><Item id="0"><Model><![CDATA[AAS3602I]]></Model><Quantity>1</Quantity><DealerPrice>226.000</DealerPrice><NetPrice>226.000</NetPrice></Item></OrderDetail>
ignore my last post.
so any ideas?
Either that isn't your "raw" JSON or your GetSaleTax web service is returning XML and not JSON.  I would need to see the code of your web service to help further.  Does this web service work for any other applications?

Or I can show you how to parse that data as XML not JSON.
the web service return as string, and string will be xml format.
Yes, you can show me how to pars the data as xml not json.

As long as this error is not occurred. It is good to go.

Question: so regardless of which datatype will be response, if assume the web service response datatype as xml,
and in jquery code I code for json datatype, it is still generated an error as cross domain issue?
Below is web service (wcf) codes if you want to read.

#region GetSaleTax
        public string GetSaleTax(string CompanyID, string CustomerNo, string ShipToCity, string ShipToState, string ShipToZip, string ShipToCountry)
        {
            #region procedures
            try
            {
            string body = string.Format(@"<SOAP:Envelope><SOAP:Header></SOAP:Header>
            <SOAP:Body>
            <m:Retrieve>
            <itemid>
            <GenerateReportRequest>
            <ReportType>CUST-SALESTAX</ReportType>
            <ReportFormat>XML</ReportFormat>
            <ReportParams>
            <CompanyNumber>{0}</CompanyNumber>
            <CustomerNumber>{1}</CustomerNumber>
            <ShipToCity>{2}</ShipToCity>
            <ShipToState>{3}</ShipToState>
            <ShipToZip>{4}</ShipToZip>
            <ShipToCountry>{5}</ShipToCountry>
            </ReportParams>
            </GenerateReportRequest>
            </itemid>
            </m:Retrieve>
            </SOAP:Body>
            </SOAP:Envelope>", CompanyID, CustomerNo,ShipToCity,ShipToState,ShipToZip,ShipToCountry);
                abc.B2B.ERP.jBase.Helper.Retrieve d = new Kenwood.B2B.ERP.jBase.Helper.Retrieve(body);
                XmlDocument xml = new XmlDocument();
                xml.LoadXml(d.GetData());
                string node = xml.SelectSingleNode("//RetrieveResult").InnerXml;
                return node;
            }
            catch (Exception ex)
            {
                //ILog log = LogManager.GetLogger("");
                //log.Error(m => m("Error:", ex.ToString()));
                throw ex;
            }
            #endregion
        }
        #endregion
here is the interface

 #region GetSaleTax
        [OperationContract]
        [WebGet(UriTemplate = "ProQuoteServiceJVC/GetSaleTax({CompanyID},{CustomerNo},{ShipToCity},{ShipToState},{ShipToZip},{ShipToCountry})")]
        String GetSaleTax(string CompanyID, string CustomerNo, string ShipToCity, string ShipToState, string ShipToZip, string ShipToCountry);
        #endregion
ignore previous one. look below one

 #region GetSaleTax
        [OperationContract]
        [WebGet(UriTemplate = "QuoteService/GetSaleTax({CompanyID},{CustomerNo},{ShipToCity},{ShipToState},{ShipToZip},{ShipToCountry})")]
        String GetSaleTax(string CompanyID, string CustomerNo, string ShipToCity, string ShipToState, string ShipToZip, string ShipToCountry);
        #endregion
any updates?
ASKER CERTIFIED SOLUTION
Avatar of ambience
ambience
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
can you show me in codes? I do not know much about the plug in you mentioned.

or

you can show me how to make the existing web service converting to json based.