Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery.ajax returns 400 Bad Request

Tags:

jquery

ajax

This works fine:

jQuery('#my_get_related_keywords').click(function() {
    if (jQuery('#my_keyword').val() == '') return false;
        jQuery.getJSON("http://boss.yahooapis.com/ysearch/web/v1/"
        +jQuery('#my_keyword').val()+"?"
        +"appid=myAppID"
        +"&lang=en"
        +"&format=json"
        +"&count=50"
        +"&view=keyterms"
        +"&callback=?",
        function (data) {//do something}

This returns 400 Bad Request (Just a reformulation of the above jQuery using .ajax to support error handling).

jQuery('#my_get_related_keywords').click(function()
    {
    if (jQuery('#my_keyword').val() == '') return false; 
    jQuery('#my_loader').show();
    jQuery.ajax(
        {
        url: "http://boss.yahooapis.com/ysearch/web/v1/"
        +jQuery('#my_keyword').val()+"?"
        +"appid=myAppID"
        +"&lang=en"
        +"&format=json"
        +"&count=50"
        +"&view=keyterms"
        +"&callback=?", 
        success: function(data)
            {//do something}
like image 933
Scott B Avatar asked Nov 11 '10 16:11

Scott B


2 Answers

I think you just need to add 2 more options (contentType and dataType):

$('#my_get_related_keywords').click(function() {

    $.ajax({
            type: "POST",
            url: "HERE PUT THE PATH OF YOUR SERVICE OR PAGE",
            data: '{"HERE YOU CAN PUT DATA TO PASS AT THE SERVICE"}',
            contentType: "application/json; charset=utf-8", // this
            dataType: "json", // and this
            success: function (msg) {
               //do something
            },
            error: function (errormessage) {
                //do something else
            }
        });
}
like image 114
Andrea Turri Avatar answered Oct 25 '22 09:10

Andrea Turri


Add this to your ajax call:

contentType: "application/json; charset=utf-8",
dataType: "json"
like image 22
light Avatar answered Oct 25 '22 08:10

light