locked
Request Error - 400 RRS feed

  • Question

  • User219851468 posted

    I dont know where to start with this question.

    I simply want to submit a json via jquery ajax to my service through POST method. Process it and return a new json to the web browser.

    The jquery from the web page

     $("#generateTrialButton").click(function (event) {
    
                     callServerGenerateTrial();
                 });
    
                 var callServerGenerateTrial = function () {
    
                     debugger;
    
                   //  alert("asd");
                     var entityId = $("#leadId").val();
                     var leadSalutation = $("#leadSalutation").val(); 
                     var leadFirstName = $("#leadFirstName").val(); 
                     var leadLastName = $("#leadLastName").val(); 
                     var leadWebsite = $("#leadWebsite").val(); 
                     var leadCompany = $("#leadCompany").val(); 
                     var leadEmail = $("#leadEmail").val(); 
                     var leadPhone = $("#leadPhone").val();
                     var leadReferred_By__c = $("#leadReferred_By__c").val();
    
                     
                     var leadOwnerId = $("#leadOwnerId").val(); 
                     var leadTrial_link__c = $("#leadTrial_link__c").val();
                     var leadCountry = $("#leadCountry").val(); 
                     
                                
    
                     var oauth = readCookie("OAuth");
    
                     var userJson = $("#userJson").val();
               
               
                     $.ajax({
                            type: "POST",
                            traditional: true,
                             url: "http://xxx.com/restapi/mrs.svc/gt",
                             contentType: "application/json; charset=utf-8",
                            data: JSON.stringify({
                                  "entityId": entityId, "Salutation": leadSalutation,
                                 "FirstName": leadFirstName, "LastName": leadLastName, "Website": leadWebsite,
                                 "companyName": leadCompany, "Email": leadEmail, "Phone": leadPhone, "owner":leadOwnerId,
                                 "trial_link__c": leadTrial_link__c, 'linkExpiry': '',
                                 "Country": leadCountry,"oauth": oauth
                             })
    
                    }).done(function(data) {
    
                             for (var property in data) {
                                 alert(data[property]);
                             }
    
                         })
                         .fail(function (jqXHR, error, errorThrown) {
                             alert("Something "+jqXHR.responseText);
                             alert(jqXHR.status);
                         });
    
                 };

    The service wrapper

    namespace gs.restapi
    {
       // [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)]
        [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
        public class mrs : Imrs 
        {
    
            [WebMethod]
            public object gt(string JsonFormData)
            {
                return msHelper.gt(JsonFormData);
    
    
            }
    }
    }

    the msHelper method

     public static object gt(String JsonFormData)
            {
    
              var someData = new
                {
                    email = "ilan.perez@gmail.com",
                    key = "asd"
                };
    
    
    
                            JavaScriptSerializer json_serializer = new JavaScriptSerializer();
                            object routes_list = json_serializer.Serialize(someData );
                            return routes_list;
                            
                            }
                            return "-3";
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex, "Error");
                        return ex;
                        
                    }
    
              
    
            }

    I always get a Request Error.

    Any help or direction would be much apprecaited!

    Wednesday, October 26, 2016 9:45 AM

Answers

  • User475983607 posted

    Not resolved...just a different problem to what i thought :)

    You're sending a JSON object to the web method not a string named "JsonFormData"

    data: JSON.stringify({
    	  "entityId": entityId, "Salutation": leadSalutation,
    	 "FirstName": leadFirstName, "LastName": leadLastName, "Website": leadWebsite,
    	 "companyName": leadCompany, "Email": leadEmail, "Phone": leadPhone, "owner":leadOwnerId,
    	 "trial_link__c": leadTrial_link__c, 'linkExpiry': '',
    	 "Country": leadCountry,"oauth": oauth
     })

    You could do this...

    data: JSON.stringify(
    	  "entityId": entityId, "Salutation": leadSalutation,
    	 "FirstName": leadFirstName, "LastName": leadLastName, "Website": leadWebsite,
    	 "companyName": leadCompany, "Email": leadEmail, "Phone": leadPhone, "owner":leadOwnerId,
    	 "trial_link__c": leadTrial_link__c, 'linkExpiry': '',
    	 "Country": leadCountry,"oauth": oauth
     );

    And update your method signature to include; entityId, Salutation...

    Or you better yet create a class that has properties; entityId, Salutation... And pass that to the method.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 26, 2016 12:45 PM

All replies

  • User-691209617 posted

    Hi,

    Can you please go to browser control and check network tab and explore the request.

    http://xxx.com/restapi/mrs.svc/gt

    Paste this request in browser and check whether any exception or error came in front of you.

    Please share details with me for further investigation.

    Hope it helps.

    Wednesday, October 26, 2016 10:33 AM
  • User219851468 posted

    405 (Method not allowed)

    Wednesday, October 26, 2016 10:52 AM
  • User219851468 posted

    This is what i have in my web config

    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />
        <!--`
            To browse web app root directory during debugging, set the value below to true.
            Set to false before deployment to avoid disclosing web app folder information.
          -->
        <directoryBrowse enabled="true" />
            <httpProtocol>
                <customHeaders>
                    <add name="Access-Control-Allow-Headers" value="Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With" />
                    <add name="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,OPTIONS" />
                    <add name="Access-Control-Allow-Origin" value="*" />
                </customHeaders>
            </httpProtocol>
      <handlers>
          <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
          <remove name="OPTIONSVerbHandler" />
          <remove name="TRACEVerbHandler" />
          <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
        </handlers></system.webServer>

    Wednesday, October 26, 2016 10:59 AM
  • User-691209617 posted

    Hmm, it might due to couple of reasons . I am giving you a link please try all scenarios.

    http://stackoverflow.com/questions/15718741/405-method-not-allowed-web-api

    Wednesday, October 26, 2016 11:01 AM
  • User219851468 posted

    So as it turns out after much debugging that the data is arriving as NULL to the service. This is the issue.

    Wednesday, October 26, 2016 11:47 AM
  • User-691209617 posted

    Great to hear. Your problem is resolved. Please mark this thread as answer so that it may help others.

    Wednesday, October 26, 2016 12:29 PM
  • User219851468 posted

    Not resolved...just a different problem to what i thought :)

    Wednesday, October 26, 2016 12:30 PM
  • User475983607 posted

    Not resolved...just a different problem to what i thought :)

    You're sending a JSON object to the web method not a string named "JsonFormData"

    data: JSON.stringify({
    	  "entityId": entityId, "Salutation": leadSalutation,
    	 "FirstName": leadFirstName, "LastName": leadLastName, "Website": leadWebsite,
    	 "companyName": leadCompany, "Email": leadEmail, "Phone": leadPhone, "owner":leadOwnerId,
    	 "trial_link__c": leadTrial_link__c, 'linkExpiry': '',
    	 "Country": leadCountry,"oauth": oauth
     })

    You could do this...

    data: JSON.stringify(
    	  "entityId": entityId, "Salutation": leadSalutation,
    	 "FirstName": leadFirstName, "LastName": leadLastName, "Website": leadWebsite,
    	 "companyName": leadCompany, "Email": leadEmail, "Phone": leadPhone, "owner":leadOwnerId,
    	 "trial_link__c": leadTrial_link__c, 'linkExpiry': '',
    	 "Country": leadCountry,"oauth": oauth
     );

    And update your method signature to include; entityId, Salutation...

    Or you better yet create a class that has properties; entityId, Salutation... And pass that to the method.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 26, 2016 12:45 PM
  • User219851468 posted

    Building a class for the object solved the issue.

    Data is being POSTed correctly. Just not returned :(

    Getting closer

    Wednesday, October 26, 2016 1:33 PM
  • User475983607 posted

    Building a class for the object solved the issue.

    Data is being POSTed correctly. Just not returned :(

    Getting closer

    I'm not sure what the updated code looks like but  deserializing the JSON data is unnecessary because the framework does it for you when you pass an object.  Simply access the values with dot syntax.

    Wednesday, October 26, 2016 1:44 PM
  • User-691209617 posted

    I prefer use fiddler tool in order to see data over HTTP. It will give you a clear idea of get and post requests.

    Wednesday, October 26, 2016 1:45 PM