Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts

    JSON request submitted using form and jQuery

    I am trying to submit a JSON request to a Tranzact api using jQuery and AJAX. Right now it keeps coming up as unsuccessful. I cant figure out how to print the results to see if it matches this provided:
    {"Authentication":{"UserName":"test","Password":"test"},”Site”:”FortLee”,"LeadDatetime":"2013-01-16T17:55:49.5229485-05:00","SourceTransactionId":"1234567","FirstName":"Test",”MiddleName”:”Joe”,”LastName":"Test",”Lead Router”:”walmart”,"EmailAddress":"test@test.com", “PhoneNumber”:”201333999”,”AltPhoneNumber”:”2012423231”,"AgentCallBackRequested":”Yes”,"PilotFlagInd icator":"Y",” WalmartDiscountCode “:”23456789”,"CampaignAttributes":null,” PreferredTimeToContact”:null,” PreferredMethodOfContact”:null,”LeadSource”:”UPS”,”MarketerId”:”UPS”,”Address”:{ ”AddressLine1”:”123 Main St”,”AddressLine2”:null,"City”:”Stafford”,”State”:”TX”,”zip”:”77477”}}

    Supposed to get
    Content:
    {"Success":"true","LeadId":"XXXX"}

    I get:
    { "Success": "false", "LeadId": "0", "errorText": "Unexpected error occured" }

    Any help is appreciated.

    Code:
    <html>
    <title>Test JSON form</title>
    <head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript">
    
    $(function(){
      $("form").submit(function(){
        $.post($(this).attr("action"), $(this).serialize(), function(jsonData){
          console.log(jsonData);
        }, "json");
      });
    });
    
    
    
    
    </script>
    
    <style>
    form {
        line-height: 2em;
    }
    p {
        margin: 5px 0;
    }
    h2 {
        margin: 10px 0;
        font-size: 1.2em;
        font-weight: bold
    }
    #result {
        margin: 10px;
        background: #eee;
        padding: 10px;
        height: 40px;
        overflow: auto;
    }
    </style>
    
    </head>
    <body>
    
    <h2>Form</h2>
        <form action="http://qa.oms.autoinsurance.com/api/lead/post" method="post" id="myForm" >
            Username:<input type="text" name="UserName" maxlength="36" size="12"/> <br/>
            password:<input type="text" name="Password" maxlength="36" size="12"/> <br/>
            LeadDatetime:<input type="text" name="LeadDatetime" maxlength="36" size="12"/> <br/>
            FirstName:<input type="text" name="FirstName" maxlength="36" size="12"/> <br/>
            LastName:<input type="text" name="LastName" maxlength="36" size="12"/> <br/>
            State:<input type="text" name="State" maxlength="36" size="12"/> <br/>
            Zip:<input type="text" name="Zip" maxlength="36" size="12"/> <br/>
            LeadRouter:<input type="text" name="LeadRouter" maxlength="36" size="12" value="‘walmart’"/> <br/>
            PhoneNumber:<input type="text" name="PhoneNumber" maxlength="36" size="12"/> <br/>
            LeadSource:<input type="text" name="LeadSource" maxlength="36" size="12" value="'UPS’"/> <br/>
            MarketerId:<input type="text" name="MarketerId" maxlength="36" size="12" value="'UPS’"/> <br/>
            <p><input type="submit" id="submitButton" /></p>
        </form>
        <h2>JSON</h2>
    <pre id="result">
    </pre>
    
    
    </body>
    </html>

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    First, since you're doing ajax to submit the form, you should add return false at the end of the submit event handler to prevent the form from being submitted.

    If the error persists, then check the post data you are passing to the ajax request. Make sure the name and values are what the api expects. I noticed that the LeadSource and MarketerId fields have value="'UPS'". LeadRouter field has single quotes in the value too. Are you sure you need the single quotes there?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •