javascript - 将值传递给 ajax 函数

标签 javascript jquery ajax

我正在我的第一个应用程序中工作,我正在尝试将值传递给 ajax 函数,但我不知道如何继续。

请帮忙吗?

这是我的代码:

html:这是必须传递值的链接,例如“9”

<a id="btnRockArg" data-role="button" data-transition="slide" href="category.html">Categor&iacute;as</a>

功能

category: function(){

    function getCategory(valueCatRockArg) {
        var dfd = $.Deferred();
        $.ajax({        
            url: 'http://.....org/api/get_category_posts?id='+valueCatRockArg+'&count=5&order=desc',
            type: 'GET',
            dataType: 'jsonp',
            success: function(data){               
                var source   = $("#category-template").html(); //Get the HTML from the template in the script tag
                var template = Handlebars.compile(source); // compilamos la plantilla
                var blogData = template(data); // en data se almacena el contenido que nos viene de wordpress
                $('#category-data').html(blogData); // mostramos el contenido
                $('#category-data').trigger('create');
                dfd.resolve(data);

            },

            error: function(data){
                console.log(data);
            }
        });

        return dfd.promise();
    };

    getCategory().then(function(data){
        $('#all-categories').on('click','li', function(e){                
            localStorage.setItem('postData', JSON.stringify(data.posts[$(this).index()]));
        });
    });


},

非常感谢哟!!

最佳答案

$.ajax({        
        url: 'http://.....org/api/get_category_posts',
        data:'id='+valueCatRockArg+'&count=5&order=desc',
        type: 'GET',
        dataType: 'jsonp',
        success: function(data){               
            var source   = $("#category-template").html(); //Get the HTML from the template in the script tag
            var template = Handlebars.compile(source); // compilamos la plantilla
            var blogData = template(data); // en data se almacena el contenido que nos viene de wordpress
            $('#category-data').html(blogData); // mostramos el contenido
            $('#category-data').trigger('create');
            dfd.resolve(data);

        },

        error: function(data){
            console.log(data);
        }
    });

试试这个,使用数据属性来传递参数

关于javascript - 将值传递给 ajax 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21279825/

相关文章:

javascript - 在 html 文档的开头声明变量

jquery - FullCalendar 上的默认值(拖放事件)

php - 在通过 ajax 加载页面/内容后初始化 video.js

javascript - 两个 DIVS,一个流体,一个固定,无论高度如何,固定 div 内部都有垂直居中的元素

javascript - Angular 7,没有 NgModel 如何获取复选框状态(真/假)?

javascript - jquery 引用出错并同时获取两个版本的 jquery

javascript - 在 jquery 中使用字符串键将元素添加到数组

javascript - 嵌套内容可编辑的按键事件 (jQuery)

jquery - 如何在 native iOS 应用程序中添加 AJAX 组件

javascript - 如何检查页面中所有ajax函数是否加载完成?