【问题标题】:JQuery Ajax request not sending through post variablesJQuery Ajax 请求不通过 post 变量发送
【发布时间】:2014-01-30 11:39:24
【问题描述】:

当将类型更改为 GET 时,我很难使用 JQuery Ajax 函数通过 post 变量发送。

代码

function loadAttributes() {
            var selectedValues = [];    
            var presentValues = [];    
            $('#attrplaceholder div').each(function(){
                if (typeof $(this).data('attrid') !== 'undefined')
                {
                    presentValues.push($(this).data('attrid'));
                } 
             });
            $("#categories :selected").each(function(){
                   selectedValues.push($(this).val()); 
            });



            jQuery.ajax({
               url: 'backend.php?r=products/setProductAttributes',
               type: "POST",
               dataType:"html",
                contentType: 'text/html; charset=utf-8',
               data: {ids: selectedValues,presentids: presentValues},
               success: function(json){
alert(json)
                        if(json!='[]'){
                            var obj = eval ("(" + json + ")"); 
                            if(obj.json !== 'undefined'){
                                    $.each(obj.json, function(k, v) {
                                        if(k==='views'){
                                            $("#attrplaceholder").append(v);
                                        }
                                        if(k==='rem'){
                                            var remvals = v.toString().split(",");
                                            for(i=0;i<remvals.length;i++){
                                                $("#attrgrp_"+remvals[i]).remove();
                                            }

                                        }
                                    });
                            }
                        }
               }
               });
            return false;
         }

        loadAttributes() 

        $("#categories").on("change",function(){
            loadAttributes() 
        });

任何指针都会有所帮助,我尝试使用 contentTypes 但仍然没有运气

【问题讨论】:

  • 如果您使用 Firebug 或 Chrome 开发工具,您能否检查网络调用并查看您的 AJAX 调用是否包含请求负载中的数据?
  • 添加error回调函数,并记录textStatus
  • @DavidHoerster - 请求负载发送以下数据 ids%5B%5D=6&ids%5B%5D=16&ids%5B%5D=13 ,因此正在发送数据

标签: jquery


【解决方案1】:

contentType 是您发送到服务器端的数据格式。

根据您的情况,您应该将其设置为:application/x-www-form-urlencoded; charset=UTF-8 或 application/json; charset=UTF-8,具体取决于您的服务器端期望的格式

或完全删除它,因为默认情况下contentType 是application/x-www-form-urlencoded; charset=UTF-8

Differences between contentType and dataType in jQuery ajax function

【讨论】:

  • 谢谢,应用程序/x-www-form-urlencoded; charset=UTF-8 似乎已经解决了这个问题,我从来没有尝试过这个问题,也没有通过省略选项来使用 contentType 的默认值
猜你喜欢
  • 2016-01-21
  • 2014-05-09
  • 2021-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-05
相关资源
最近更新 更多