Error when using $.ajax() and Create an XMLHttpRequest object in the same page
Hi all,
I fetch information from database using ajax , one time using
$.ajax() and the other by create
XMLHttpRequest object in the same page ,
my code looks like this :
HTML code :
- <button class='orderBtn' >order</button>
- <!--here some html code-- >
- <ul id='item-nav'>
- <li ><a cate_id='1'>item_1 </a></li>
- <li ><a cate_id='2'>item_2 </a></li>
- </ul>
- <div id="items_container"></div>
_javascript code :
- $(document).ready(function() {
-
- //here some code
-
- $("ul#item-nav li").click(function() {
-
- //here some code
- setTimeout(function() {
- showItems(code,id);
- }, 300);
-
- });
-
- //here i included the order_fn.js file where i used $.ajax()
- $.getScript("js/order_fn.js");
-
- })
-
- //get data using ajax__2
- function showItems(str,id) {
-
- document.getElementById("items_container").innerHTML = "";
-
- if (str == "") {
- document.getElementById("items_container").innerHTML = "";
- return;
- } else {
- if (window.XMLHttpRequest) {
- // code for IE7+, Firefox, Chrome, Opera, Safari
- xmlhttp = new XMLHttpRequest();
- } else {
- // code for IE6, IE5
- xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
- }
-
- xmlhttp.onreadystatechange = function() {
- if (xmlhttp.readyState == 4 && xmlhttp.status == 200 ) {
-
- //by reloading order_fn.js my code works well
- $.getScript("js/order_fn.js");
-
- document.getElementById("items_container").innerHTML = xmlhttp.responseText;
-
- }
- }
-
- xmlhttp.open("GET","getcateg.php?cate_code="+str+"&cateID="+id,true,function() {
-
- });
- xmlhttp.send();
-
-
- }
- }
order_fn.js file :
- //get data using ajax__1
- $('.orderBtn').click(function (){
-
-
- //some code
- $.ajax({
- url: 'file_name.php',
- data:formData,
- dataType: "json",
- type: 'GET',
- success: function(data) {
- //do something here
- }
- });
-
- });
my code was work well before I expand it with using XMLHttpRequest.
When i used XMLHttpRequest the click event on .orderBtn button does not work after execute the ajax event using XMLHttpRequest.
so I Dismissed it in a separate file "order_fn.js" file ,because it need to use the instruction
- $.getScript("js/order_fn.js");
two time ,one to include the code and the other to reload it after execute the ajax event using XMLHttpRequest.
my question is ,Is this the best way to resolve that problem?
I need your guidance in how to improve my code.
thanks.