locked
On Ajax Post, request going as a Bad Request(404) RRS feed

  • Question

  • User-587451972 posted
    Hi,  
    
    I am doing post call using jquery ajax, but on post-call, I am getting a bad request.    
          
    var data = JSON.stringify(dataArr);         
    var clientType = $("#clientType").val();         
    var username = $("#hidUsername").val();         
    var clientId = $("#clientId").val();         
    var apiUrl = 'http://abc.com/WebAPI/client/PostToclient'                  
    $.ajax({             
    url: apiUrl,             
    type: 'POST',             
    //contentType: 'application/json; charset=utf-8',             
    dataType: "json",             
    data:JSON.stringify( { 'clientData': data, 'username': username, 'Id': clientId ,'clientType':clientType}),             
    cache: false,
                success: function (response) {
                    msg.attr('class', 'SuccessMessage');
                    msg.text(response.d);
    
                },
                complete: function () {
    
                },
                error: function (ex) {
                    msg.attr('class', 'ErrorMessage');
                    if (ex != undefined && ex.responseText != undefined) {
                        try {
                            var response = jQuery.parseJSON(ex.responseText);
                            msg.text(response.Message)
                        }
                        catch (error) {
                            msg.text(ex.responseText);
                        }
                    }
                }
            });
    
    
    Note: on using contentType: 'application/json; charset=utf-8', on post my request going as a "OPTIONS"
    
    Thanks In Advance 

    Saturday, June 1, 2019 3:09 PM

All replies

  • User753101303 posted

    Hi,

    This is "404 Not Found" and "400 Bad Request". Could you confirm which one you have ?

    You are 100% sure this is http://abc.com/WebAPI/client/PostToclient rather than for example http://abc.com/api/client ?

    Sunday, June 2, 2019 12:27 PM
  • User665608656 posted

    Hi archer,

    According to your description, bad request is 400 errors, which is different from 404 error.

    The 400 Bad Request Error is an HTTP response status code that indicates that the server was unable to process the request sent by the client due to invalid syntax. 

    Here are some of the most likely causes of 400 Bad Request Error:

    1.The client may be accidentally (or intentionally) sending deceptive request routing information. Some web applications/web servers look for custom HTTP headers to process requests and verify the client isn’t attempting anything malicious. If an expected custom HTTP header is missing or invalid, a 400 Bad Request Error is a likely result.

    2.The client may be uploading a file that is too large. Most web servers or applications have an explicit file size limit that prevents files that are too big from being uploaded and clogging up bandwidth and other resources in the server. In many cases, the server will produce a 400 Bad Request Error when a file is too large (and, thus, the request cannot be completed).

    3.The client is accessing an invalid URL. If the client is sending a request to an invalid URL — particularly one that is malformed via improper characters — this could result in a 400 Bad Request Error.

    4.The client is using an invalid or expired local cookie. Again, this could be malicious or accidental, but it’s possible that a local cookie in the web browser is identifying you via a session cookie. If this particular session token matches the session token from another request from a different client, the server/application may see this is a malicious act and produce a 400 Bad Request Error code.

    The HTTP 404, 404 Not Found, 404, Page Not Found, or Server Not Found error message is a Hypertext Transfer Protocol (HTTP) standard response code, in computer network communications, to indicate that the browser was able to communicate with a given server, but the server could not find what was requested.

    To solve 400 error , You need to examine these possibilities:

    1.At the very least, make sure your 404 error page has links to other sections of your site. One example is a link to your site map.

    2.If your site or blog has a search engine, make sure it is visible on the 404 error page.

    3.If you are using Google Analytics, set up error tracking so you can know about your 404 errors. 404 errors are also in the Google Webmaster tools.

    4.If you are familiar with a scripting language, instead of referring a static 404 page, make it dynamic. For example, the Computer Hope 404 page logs each error, runs the URL through an algorithm to suggest a recommended page, and finds keywords in the URL to be used in the search box.

    For more details,you could refer to these links:

    https://airbrake.io/blog/http-errors/400-bad-request

    https://en.wikipedia.org/wiki/HTTP_404

    If this still don't help you solve your issue, please provide more detailed information and code, which will help us solve your issue more easierly.

    Best Regards,

    YongQing.

    Monday, June 3, 2019 7:29 AM
  • User-474980206 posted

    a bad request means invalid data. in you case this means the json is not correct, or the api uses the content-type to determine the datatype and you are passing the jquery default of form url encoded.

    if it fails when you specify content-type json, than the json format is wrong. note, json is case sensitive.

    Thursday, June 13, 2019 3:18 PM