Jquery,Ajax වලින් Web Service එක Access කරන්න බැ. හදා ගන්නේ කොහමද??

Seven Net

Active member
  • Jan 13, 2014
    290
    62
    28
    30
    Earth
    Jquery,Ajax වලින් Web Service එක Access කරන්න බැ. හදා ගන්නේ කොහමද??

    Jquery,Ajax වලින් Web Service එක Access කරන්න බැ. හදා ගන්නේ කොහමද??

    මම දැනට වෙබ් එකක් හදනවා. එක Data ගන්නේ SOAP Web Service එකකින්. අදාල Inputs ලබා දිලා , Search එක ක්ලික් කරාම , වෙබ් බ්‍රව්සර් එකේ Console Tab එකේ "
    Failed to load http://holidaywebservice.com/HolidayService_v2/HolidayService2.asmx?WSDL: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
    " කියල පෙන්වනවා. හදා ගන්න උත්සහා කරන්න පුලුවන් හැම විධියක්ම බැලුව , ඒත් මුකුත් සෙට් වුනේ නැ. අන්තිමට Chrome එකට මේ Extension එක දාල තමයි Test කරේ.

    ඒත් මට ඕන Extension භාවිතා නොකර මේක හදා ගන්නේ කොහමද කියල දැන ගන්නයි. උදව් කරන්න පුලුවන් නම් ලොකු දේයක්.

    මේ පහල විධියට තමයි , Request එක යවන්නේ.


    Code:
    <script type="text/javascript">
                    $(document).ready(function () {
    
                        $('#ghsubmitbtn').on('click', function (e) {
                            var country = $('#country').val();
                            var year = $('#year').val();
                            var month = $('#month').val();
    
                            //console.log(departureDate);
    
                            var str = '<?xml version = "1.0" encoding = "UTF-8"?>' +
                                '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.holidaywebservice.com/HolidayService_v2/">' +
                                '<SOAP-ENV:Body>' +
                                '<ns1:GetHolidaysForMonth>' +
                                '<ns1:countryCode>' + country + '</ns1:countryCode>' +
                                '<ns1:year>' + year + ' </ns1:year>' +
                                '<ns1:month>' + month + '</ns1:month>' +
                                '</ns1:GetHolidaysForMonth>' +
                                '</SOAP-ENV:Body>' +
                                '</SOAP-ENV:Envelope>';
    
                            //console.log(departureDate);
    
                            $.ajax({
                                url: 'http://holidaywebservice.com/HolidayService_v2/HolidayService2.asmx?WSDL',
                                method: 'POST',
                                contentType: "text/xml; charset=utf-8",
                                data: str,
                                dataType: "text",
                                crossDomain: true,
                                //headers: {"Authorization": 'Bearer ' + bat},
    
                                success: function (data) {
    
                                    console.log(data);
                                    
                                    /* var xhttp = new XMLHttpRequest();
                                    xhttp.onreadystatechange = function() {
                                        if (this.readyState == 4 && this.status == 200) {
                                            myFunction(this);
                                        }
                                    };
                                    xhttp.open("GET", "HoliRes.xml", true);
                                    xhttp.send();
    
                                    function myFunction(xml) {
                                        var xmlDoc = xml.responseXML;
                                        var x = xmlDoc.getElementsByTagName('Country')[0];
                                        var y = x.childNodes[0];
                                        
                                        var x2 = xmlDoc.getElementsByTagName('Descriptor')[0];
                                        var y2 = x2.childNodes[0];
                                        
                                        document.getElementById("demo1").innerHTML = y.nodeValue; 
                                        document.getElementById("demo2").innerHTML = y2.nodeValue; 
                                    } */
                                    
                                    document.getElementById("demo1").innerHTML=data;
    
                                    var xml = data,
                                      xmlDoc = $.parseXML( xml ),
                                      $xml = $( xmlDoc ),
                                      
                                      $CountryXML = $xml.find( "Country" )[0].childNodes[0].nodeValue;
                                      $( "#Country" ).append( $CountryXML );
                                      //document.getElementById("Country").innerHTML = $y.nodeValue; 
                                      
                                      //$HolidayCode = $xml.find( "HolidayCode" );
                                      //$( "#HolidayCode" ).append( $HolidayCode.text() );
    
                                      $HolidayCodeXML = $xml.find( "HolidayCode" )[0].childNodes[0].nodeValue;
                                      $( "#HolidayCode" ).append( $HolidayCodeXML );
                                      
                                      $DescriptorXML = $xml.find( "Descriptor" )[0].childNodes[0].nodeValue;
                                      $( "#Descriptor" ).append( $DescriptorXML );
                                      
                                      $BankHolidayXML = $xml.find( "BankHoliday" )[0].childNodes[0].nodeValue;
                                      $( "#BankHoliday" ).append( $BankHolidayXML );
                                      
                                      $DateXML = $xml.find( "Date" )[0].childNodes[0].nodeValue;
                                      $( "#Date" ).append( $DateXML );
                                      
                                      var BookBtn = '<input type="submit" value="Book" name="book" class="btn btn-primary">';
    
                                        for (var v = 0; v <= 999; v++) {
                                        var row = $('<tr><td>' + $xml.find( "Country" )[v].childNodes[0].nodeValue + '</td><td>'
                                            + $xml.find( "HolidayCode" )[v].childNodes[0].nodeValue + '</td><td>'
                                            + $xml.find( "Descriptor" )[v].childNodes[0].nodeValue + '</td><td>'
                                            + $xml.find( "BankHoliday" )[v].childNodes[0].nodeValue + '</td><td>'
                                            + $xml.find( "Date" )[v].childNodes[0].nodeValue + '</td><td>'
                                            + BookBtn + '</td></tr>');
                                        $('#tblData').append(row);
                                        //$('#tblData').empty();
                                        //$("#tblData").empty().$('#tblData').append(row);
                                        }
    
                                        //$('#tblData').load(row);                                
                                }
    
                            }); //Ajax
    
                        }); //Button
    
                    });
     
    Last edited:

    Seven Net

    Active member
  • Jan 13, 2014
    290
    62
    28
    30
    Earth
    හෙනයා;23552798 said:

    video එකේ විදිහට කරලා බලන්න

    මේකේ කරන දේ තේරේන් නැනේ.. ඒ කිව්වේ , ඕක මගේ එකට ගන්න විධිය..
     

    Seven Net

    Active member
  • Jan 13, 2014
    290
    62
    28
    30
    Earth
    හෙනයා;23552937 said:

    අර header 2 request එකට bind කරලා යවන්න

    කොහමද Headers 2 Bind කරන්නේ ?? කොහම කරත් වැඩේ වෙන්නේ නැනේ.. :/
     

    හෙනයා

    Well-known member
  • May 23, 2014
    16,897
    17,125
    113
    Kottawa
    කොහමද Headers 2 Bind කරන්නේ ?? කොහම කරත් වැඩේ වෙන්නේ නැනේ.. :/


    හප්පට.. මට පැටලුනා.. ඒ server එකෙන් එන response එකට දාන්නේ.. පොඩ්ඩක් ඉදහන් බලන්න
     

    Seven Net

    Active member
  • Jan 13, 2014
    290
    62
    28
    30
    Earth
    හෙනයා;23553109 said:

    හප්පට.. මට පැටලුනා.. ඒ server එකෙන් එන response එකට දාන්නේ.. පොඩ්ඩක් ඉදහන් බලන්න

    මොකක් හරි වැඩේ වෙන විධියක් තමයි ඕන..
     

    maxtalker

    Well-known member
  • Oct 7, 2007
    7,401
    13,043
    113
    උඹ Cross Domain ද ගහන්නේ ? එහෙම ඕන නං API out එක Json විදිහට එන්න ඕන. නැත්තං උඹට කරන්න තියෙන්නේ Ajax request එක php handler එකකට ගහලා php handler එකෙන් CURL කරන්න.