1

I am having a problem trying to get ajax functioning properly, everytime I do a call i get

An AJAX HTTP error occurred.
HTTP Result Code: 500
Debugging information follows.
Path: /system/ajax
StatusText: Internal Server Error
ResponseText: 

What am i trying to do is have the drop down fill with the results of a function from api. The function works perfectly it gets the results when needed however the problem is with the ajax I even try to print out hello world and still got the 500 error.

this is the code I currently have, any advice is appreciated.

function autotitleform($form, &$form_state){

    $form['vehicle_info'] = array(
        '#type' => ('fieldset'),
        '#title' => t('Vehicle Information'),
    );

    $form['autoinfo']['vehicle_year'] = array(
        '#type' => 'select',
        '#title' => t('Vehicle Year'),
        '#options' => auto_title_year_api(),
        '#ajax' => array(
            'callback' => 'auto_title_make_api_callback',
            'wrapper' => 'api_make'
        ),
    );

    $form['autoinfo']['wrapper'] = array(
        '#prefix' => '<div id="api_make">',
        '#suffix' => '</div>',
    );



    $options = array('- Select Make -');
    if(isset($form_state['values']['vehicle_year'])){
        $options = auto_title_make_api();
    }

    $form['autoinfo']['wrapper']['make_description'] = array(
        '#title' => t('City'),
        '#type' => 'select',
        '#options' => auto_title_make_api(),
  );    

    if(isset($form_state['values'])){
        $form['page_output'] = array(
        '#markup' => autotitledb_submit($form, $form_state)
        ); 
    }
    return $form;
}


function auto_title_make_api_callback($form, $form_state) {
  return $form['autoinfo']['wrapper'];
}

function auto_title_make_api(){
$url_make = 'https://test.api.800loanmart.com/Loanmartservice.svc/soapssl'; // SOAP 1.1 
$post_str_make = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<GetVehicleMakes xmlns="http://tempuri.org/">
<customerId>73c858f0-791c-454e-b4d5-5634bf13621d</customerId>
<authenticationId>3074940f-7efe-447c-a7ff-3c152acdf8c4</authenticationId>
<year>2000</year>
</GetVehicleMakes>
</s:Body>
</s:Envelope>';

    $headers_make = array(
        'Content-type: text/xml; charset="utf-8"',                      // For SOAP 1.1 - JHW Jan 26, 2013
        "Accept: text/xml",
        "Cache-Control: no-cache",
        "Pragma: no-cache",
        "SOAPAction: \"http://tempuri.org/ILoanmartService/GetVehicleMakes\"",
        "Content-length: ".strlen($post_str_make)       // Amount of data in bytes to be posted
    );

      $options_make = array(
          CURLOPT_RETURNTRANSFER => true,         // return web page / XML result
          CURLOPT_HEADER         => 0,          // 1/0 return/don't return headers 0 for production or 1 for debug
          CURLOPT_HTTPHEADER     => $headers_make,          // post headers
          CURLOPT_FOLLOWLOCATION => true,         // follow redirects
          CURLOPT_ENCODING       => "",           // handle all encodings

          // Browser/User Agent String
          CURLOPT_USERAGENT      => $_SERVER['HTTP_USER_AGENT'],     

          CURLOPT_AUTOREFERER    => true,         // set referer on redirect
          CURLOPT_CONNECTTIMEOUT => 120,          // timeout on connect
          CURLOPT_TIMEOUT        => 120,          // timeout on response
          CURLOPT_MAXREDIRS      => 10,           // stop after 10 redirects
          CURLOPT_POST           => 1,            // We are sending post data needs to be = '1'
          CURLOPT_POSTFIELDS     => $post_str_make,     // this is the XML post data
          CURLOPT_PORT           => 443,                    // SSL Port, probably unnesecery             

          CURLOPT_SSL_VERIFYHOST => false,        // Don't verify ssl on either side
          CURLOPT_SSL_VERIFYPEER => false,        // dito, don't verify ssl on either side

          CURLOPT_VERBOSE        => 1,            // Set to 1 for debug 
        );


    $soap_do_make = curl_init($url_make);
    curl_setopt_array($soap_do_make, $options_make);            
    $result_make = curl_exec($soap_do_make);


$sls_make = simplexml_load_string($result_make);
$sls_make->registerXPathNamespace('a', 'http://schemas.datacontract.org/2004/07/LM.API');
$sls_list_make = $sls_make->xpath('//a:MakeDescription');
foreach($sls_list_make as $sls_list_make_child){

$sls_list_make_child_result[] = sprintf("%s", $sls_list_make_child);}
dpr($sls_list_make_child_result);
$sls_list_make_child_results = array_combine($sls_list_make_child_result, $sls_list_make_child_result);

        // Close cURL
        curl_close($soap_do_make);


 return $sls_list_make_child_results;
}
function auto_title_year_api(){
$url_year = 'https://test.api.800loanmart.com/Loanmartservice.svc/soapssl'; // SOAP 1.1   
$post_str_year = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<GetVehicleYears xmlns="http://tempuri.org/">
<customerId>73c858f0-791c-454e-b4d5-5634bf13621d</customerId>
<authenticationId>3074940f-7efe-447c-a7ff-3c152acdf8c4</authenticationId>
</GetVehicleYears>
</s:Body>
</s:Envelope>';

    $headers_year = array(
        'Content-type: text/xml; charset="utf-8"',                      // For SOAP 1.1 - JHW Jan 26, 2013
        "Accept: text/xml",
        "Cache-Control: no-cache",
        "Pragma: no-cache",
        "SOAPAction: \"http://tempuri.org/ILoanmartService/GetVehicleYears\"",
        "Content-length: ".strlen($post_str_year)       // Amount of data in bytes to be posted
    );

      $options_year = array(
          CURLOPT_RETURNTRANSFER => true,         // return web page / XML result
          CURLOPT_HEADER         => 0,          // 1/0 return/don't return headers 0 for production or 1 for debug
          CURLOPT_HTTPHEADER     => $headers_year,          // post headers
          CURLOPT_FOLLOWLOCATION => true,         // follow redirects
          CURLOPT_ENCODING       => "",           // handle all encodings

          // Browser/User Agent String
          CURLOPT_USERAGENT      => $_SERVER['HTTP_USER_AGENT'],     

          CURLOPT_AUTOREFERER    => true,         // set referer on redirect
          CURLOPT_CONNECTTIMEOUT => 120,          // timeout on connect
          CURLOPT_TIMEOUT        => 120,          // timeout on response
          CURLOPT_MAXREDIRS      => 10,           // stop after 10 redirects
          CURLOPT_POST           => 1,            // We are sending post data needs to be = '1'
          CURLOPT_POSTFIELDS     => $post_str_year,     // this is the XML post data
          CURLOPT_PORT           => 443,                    // SSL Port, probably unnesecery             

          CURLOPT_SSL_VERIFYHOST => false,        // Don't verify ssl on either side
          CURLOPT_SSL_VERIFYPEER => false,        // dito, don't verify ssl on either side

          CURLOPT_VERBOSE        => 1,            // Set to 1 for debug 
        );


    $soap_do_year = curl_init($url_year);
    curl_setopt_array($soap_do_year, $options_year);            
    $result_year = curl_exec($soap_do_year);


$sls_year = simplexml_load_string($result_year);
$sls_year->registerXPathNamespace('a', 'http://schemas.datacontract.org/2004/07/LM.API');
$sls_list_year = $sls_year->xpath('//a:Year');
foreach($sls_list_year as $sls_list_year_child)
{$sls_list_year_child_result[] = sprintf("%s", $sls_list_year_child);}

$sls_list_year_child_results = array_combine($sls_list_year_child_result, $sls_list_year_child_result);

        // Close cURL
        curl_close($soap_do_year);


 return $sls_list_year_child_results;
}

Edit #1

The errors I am gettings are 
Notice: Undefined index: #options in form_select_options() (line 2661 of /home/loansdotorg/public_html/includes/form.inc).

And 

Warning: Invalid argument supplied for foreach() in form_select_options() (line 2668 of /home/loansdotorg/public_html/includes/form.inc).

Edit #2 I think I have this working although I will need to test it out more.

  • What does your server log say? Do you see a real error message if you browse directly to the AJAL URL? – mpdonadio Feb 8 '13 at 1:42
  • I took a look at the report log and it isnt giving any errors back. I am clueless as to what the problem is, and as for going to the ajax url, do you mean site.com/system/ajax? Because if so I did go to that url and it was just a white screen. Any other suggestions? – Jeremy Love Feb 8 '13 at 19:08
  • If you are getting a 500 error, then the error should be in the apache error log. The white screen is a 500 error too. Check out drupal.org/node/158043 and report back. My guess is either the cURL or the simplexml is barfing. – mpdonadio Feb 8 '13 at 23:08
  • Actually I see i am gettings errors now, I must have missed them. I posted the error above in a edit – Jeremy Love Feb 9 '13 at 3:13
  • Neither of those should generate a 500. – mpdonadio Feb 9 '13 at 14:04
1

If you look at function "form_select_options()" it also sets the selected item so set '#default' => 0 for your dropdown form variable and you wont see see that warning anymore!

$form['autoinfo']['vehicle_year'] = array(
        '#type' => 'select',
        '#title' => t('Vehicle Year'),
        '#options' => auto_title_year_api(),
        '#default' => 0, //this was missing and hence the warning, 
        '#ajax' => array(
            'callback' => 'auto_title_make_api_callback',
            'wrapper' => 'api_make'
        ),
    );
0

I'd suggest using devel module and dd() that return value just before the return to ensure it is correctly structured. with devel enabled a drupal_debug.txt file will be created on server in /tmp

-1

When I call a web service from php I do something like this:

  $client = new SoapClient("Your URL .svc?WSDL");
  $result = $client->MethodName(array('param' => 'value'));
  $result_object = $result->MethodNameResult;

Then you don't need to make the whole soap body and envelope. If you need to pass credentials in the header you can use:

$client = new SoapClient("some.wsdl", array('login'          => "some_name",
                                            'password'       => "some_password"));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.