javascript - jQuery.post 使用当前位置的修改后的 url,而不是提供的 url

标签 javascript jquery

我的 jquery.post 调用出现问题。

当我执行以下脚本时:

<script type="text/javascript">
    $(document).ready(function(){
        //var cmsr_url = 'http://127.0.0.1/sphere/modules/cmsrating/ajax.php?rand=1231';
        var cmsr_url = 'ajax/test.html';

        $('.star, .auto-submit-star').rating({
            callback: function(value, link){
                alert(cmsr_url);
                $.post({
                    url: cmsr_url,
                    // also tried to put here a string like below
                    // url: 'http://127.0.0.1/sphere/modules/cmsrating/ajax.php?rand=1231'
                    data: {rating : value},
                    success: function(){
                        alert('done!');
                    },
                    dataType: 'json'
                });
            } 

        });

    });
</script>

我的警报显示正确的网址。但是我的 Firebug 显示请求的网址是:

http://127.0.0.1/sphere/pl/content/%5Bobject%20Object%5D

它从哪里来?

最佳答案

我认为问题在于您尝试使用 $.post 函数,就像使用 $.ajax 函数一样。从文档看来,post 不接受像 $.ajax 那样的选项。 https://api.jquery.com/jQuery.post/

试试这个:

$.ajax({
    url: cmsr_url,
    // also tried to put here a string like below
    // url: 'http://127.0.0.1/sphere/modules/cmsrating/ajax.php?rand=1231'
    data: {rating : value},
    success: function(){
     alert('done!');
    },
    dataType: 'json',
    type: 'POST'
});

关于javascript - jQuery.post 使用当前位置的修改后的 url,而不是提供的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23226701/

相关文章:

javascript - 如何在基于 React 函数的 View 中进行 api 调用并填充渲染

javascript - 当用户点击目标 div 时只显示必要的 div

javascript - 淡出除鼠标指向的元素之外的所有内容

javascript - 将参数传递给状态导致元素不显示

javascript - 如何在nodejs中导出函数

javascript - Jquery 在循环内生成 Id

jquery - 导航之外没有可点击的内容(hrefs 或点击功能)

jquery - 使用 jQuery 提交表单会使浏览器崩溃

javascript - Typescript:为什么这段代码是 100% 有效的 javascript 代码却抛出异常

用于提取字符串特定部分的 Javascript 正则表达式