devbridge / jQuery-Autocomplete

Ajax Autocomplete for jQuery allows you to easily create autocomplete/autosuggest boxes for text input fields
https://www.devbridge.com/sourcery/components/jquery-autocomplete/
Other
3.56k stars 1.66k forks source link

Server response 400,ajaxSettings:{"contentType": 'application/json;charset=utf-8'} #721

Closed losnau closed 6 years ago

losnau commented 6 years ago

autocomplete Ajax config: serviceUrl: 'http://localhost:8889/cmd/get', width: 200, scrollHeight: 300,
matchContains: true,
autoFill: false,
deferRequestBy:50, paramName:'keyword',
noCache:true,
dataType: 'json', type:"POST",
params: "search",
ajaxSettings:{"contentType": 'application/json;charset=utf-8'}

However ,The server response status is 400,

In test file ,I config config ajax is that

 $.ajax({
        url: 'http://localhost:8889/cmd/get',
        data: '{"search":true,"q":"frfsdafi"}',
        type: 'post',
        dataType: 'json',
        contentType: 'application/json;charset=utf-8',
        async:false,
        complete:function(coordinates){
            result = coordinates;

            */
        }
    });

The server reponse status is 200,And I get those data.

tkirda commented 6 years ago

Looks like server side issue.

losnau commented 6 years ago

@tkirda Firstly ,I sorry to say my english is bad . = = ....! Secondly,Thanks you reply.

one

I set 'Content-Type': 'Content-Type:text/plain;;charset=utf-8' , Server get request , The data is "search=true&keyword=1234", Why not JSON?

Two

And I see from chrome begug , In Reuest Payload This is "search=true&keyword=1234" Why no JSON?

tkirda commented 6 years ago

Try adding method POST to ajaxSettings:

ajaxSettings: {
  method: 'POST' 
  // ...
}
losnau commented 6 years ago

Just no work,

   serviceUrl: '/cmd/get',
    width: 200,
    scrollHeight: 300,  
    matchContains: true,   
    autoFill: false, 
    deferRequestBy:50,
    paramName:'keyword', 
    // noCache:true, 
    // dataType: 'json',
    // type:"POST", 
    // params: {"search":true},   
    ajaxSettings:{
        headers: {
            // method: 'POST' ,
            // 'Content-Type': 'Content-Type:text/plain;;charset=utf-8'
            // "Content-Type":"application/json",
            data: '{"search":true,"q":"frfsdafi"}',
            type: 'post',
            dataType: 'json',
            method: 'POST' ,
            "Content-Type": 'application/json;charset=utf-8',
            async:false
          }},

And I find if the Reuqst Headers‘ Content-Type: is "application/x-www-form-urlencoded; charset=UTF-8" ,Server get None。 And In other ways ,If the Reuqst Headers‘ Content-Type: is "application/json;charset=UTF-8" ,Server get data 。

However,Whatever I do In ajaxSettings , the Reuqst Headers‘ Content-Type: is "application/x-www-form-urlencoded; charset=UTF-8"......

It is Crazy...

framewerks commented 6 years ago

I had a similar problem and was able to overcome it by using the the ajaxSettings parameter.

All calls to our webservice required the data param in the ajax settings to be json serialized.

This worked for me:

    ajaxSettings: {
        type: "POST",
        processData: false,
        contentType: "application/json; charset=utf-8",
        beforeSend: function (jqXHR, settings)
        {
            settings.data = JSON.stringify(settings.data);
        }
    }