Elitmiar
Elitmiar

Reputation: 36829

JQuery Ajax request not sending through post variables

I have difficulties sending through post variables using the JQuery Ajax function, when changing type to GET is all works.

CODE

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() 
        });

Any pointers will be helpful, I tried to play with the contentTypes and still no luck

Upvotes: 1

Views: 1645

Answers (1)

Khanh TO
Khanh TO

Reputation: 48972

contentType is the format of data you send to server side.

In your case, you should set it to: application/x-www-form-urlencoded; charset=UTF-8 or application/json; charset=UTF-8 depending on what format your server side expects

or remove it completely because by default contentType is application/x-www-form-urlencoded; charset=UTF-8

Differences between contentType and dataType in jQuery ajax function

Upvotes: 1

Related Questions