2017-03-12 88 views
-1

我一直在关注一个CRUD教程,位于这里https://www.codeofaninja.com/2015/06/php-crud-with-ajax-and-oop.html。 我可以创造更新和删除,但在阿贾克斯成功火灾我得到一个错误:ajax成功后我打电话给showProducts()

showProducts() not defined

这样的产品列表不会刷新。我不明白如何在没有某种引用类型的情况下找到showProducts()。我知道showProducts()的作品,因为它被称为在$(document).ready(function()

{ 
// show list of product on first load 
    showProducts(); 
}); 
works fine. 

    // send delete request to api/remote server 
    $.ajax({ 
     url: "http://localhost/api/product/delete.php", 
     type : "POST", 
     dataType : 'json', 
     data : JSON.stringify({ id: product_id }), 
     success : function(result) { 

      // re-load list of products 
      showProducts(); 
     }, 
     error: function(xhr, resp, text) { 
      console.log(xhr, resp, text); 
     } 
    }); 

} 
+1

是你的ajax和showProducts函数在同一个上下文中吗? – Roljhon

+0

“showProducts()”如何定义?你在定义之前调用它吗?在AJAX请求之前找到调用它的简单方法,以确保不会造成范围问题 – santi6291

+0

read-products.js是showProducts()的定义。 read-products.js位于index.php

0

因为您的showProducts()函数未定义

只需创建($ Ajax调用)之前之一,它添加一些数据,例如:

// function to show list of products 
function showProducts(){ 
    // you can change those contents and add whatever you like. 
    $(document).ready(function() { 
     $("#emptydiv").html("<body> Add your prodects here </body>"); 
    }); 

} 

你想要把你的数据在HTML DIV:

<div id="emptydiv"> </div> 

what we add here inside (showProducts function) is just example . you can add what ever you want (depend on if you want to follow the tutorial completely or you want to add your Owen style and content) .

  • 和你提到的教程他们的链接添加这些内容:

    // function to show list of products 
    function showProducts(){ 
    
        // get list of products from the API 
        $.getJSON("http://localhost/api/product/read.php", function(data){ 
    
        // html for listing products 
        readProductsTemplate(data, ""); 
    
        // chage page title 
        changePageTitle("Read Products"); 
    
        }); 
    } 
    
+0

的路径确定如果我把函数放在.ajax代码的前面,它会很好。现在我必须复制确切的代码并将其粘贴到以下文件 create-products.js,edit-products.js,delete.js这看起来像我在3个不同的位置复制代码。 我以为代码需要一个路径的功能showProducts() 像'read-products.js - > showProducts();' –