Quickbooks Web Connector 尝试执行时出现解析器错误 AJAX
Quickbooks Web Connector parsererror when trying to do AJAX
使用 Keith Palmer 的 类 for Quickbooks,位于此处:https://github.com/consolibyte/quickbooks-php
我现在收到从 Quickbooks Web 连接器应用程序返回的解析错误。
基本上,我正在执行一个 AJAX 请求,如下所示:
$('.form-item').on('submit', function(event) {
event.preventDefault();
var $this = $(this),
$index = $this.data('index'),
$company = $('input.company-' + $index).val(),
$status = $this.data('status');
// Current Action/Status being performed here!
console.log(current_action);
console.log($status);
var data = {
action: 'hunter_' + current_action,
security: HUNTER_admin[current_action + '_nonce'],
form: $this.serialize(), // serialize the form data
company: $company
};
// Might need to create Customer
if (current_action == 'create_estimate')
{
// This returns an array of current emails in the database and QB
data['qb_emails'] = HUNTER_admin.emails;
}
console.dir(data);
$.ajax({
type: 'POST',
url: HUNTER_admin.ajax_url,
data: data,
dataType: 'json'
}).fail(function(jqXHR, textStatus) {
alert("Request failed: " + textStatus);
}).done(function(response) {
alert('Success');
$('.' + $status + '-' + $index).replaceWith($('<div />').html(response.statustext));
}).always(function(response) {
console.dir(response);
});
现在我的 functions.php
wordpress 文件中的 ajax:
add_action('wp_ajax_hunter_create_estimate', 'hunter_create_estimate');
function hunter_create_estimate()
{
global $wpdb;
check_ajax_referer('create-estimate', 'security');
$response = array();
if (!current_user_can('manage_options'))
{
return $response;
die();
}
require_once(get_stylesheet_directory() . '/QuickBooks.php');
// Get All data!
parse_str($_POST['form'], $data);
// Check current emails in quickbooks and see if a match is found with users email, to determine if we need to create a New Customer in Quickbooks or not:
$qb_emails = !empty($_POST['qb_emails']) ? array_map('strtolower', $_POST['qb_emails']) : array();
if (empty($qb_emails) || !in_array(strtolower($data['email']), $qb_emails))
{
$data['has_email'] = false;
// Email not set within the database, so we need to add this customer to the quickbooks Queue!
$dsn = 'mysqli://' . DB_USER . ':' . DB_PASSWORD . '@' . DB_HOST . '/' . DB_NAME;
$Queue = new QuickBooks_WebConnector_Queue($dsn);
$Queue->enqueue(QUICKBOOKS_ADD_CUSTOMER);
}
else
$data['has_email'] = true;
// Now fire up the Queue that will Add the Estimate!
$response = array_merge($data, array('company' => $_POST['company'], 'statustext' => '<p class="text">Creating Estimate...</p>', 'emails' => $_POST['qb_emails']));
echo json_encode($response);
die();
}
一切正常,直到我连接 Quickbooks.php 文件并调用队列。现在我在 Quickbooks 的提交和响应期间遇到解析器错误。
实际上我只是想测试一个 QUICKBOOKS_ADD_CUSTOMER
调用:
我的地图数组如下所示:
$map = array(
QUICKBOOKS_ADD_CUSTOMER => array( '_quickbooks_customer_add_request', '_quickbooks_customer_add_response' ),
QUICKBOOKS_QUERY_CUSTOMER => array('_quickbooks_query_customer_request', '_quickbooks_query_customer_response'),
);
2 个函数如下所示:
function _quickbooks_customer_add_request($requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $version, $locale)
{
$arr = array(
'name' => 'Doe Enterprises',
'fname' => 'John',
'lname' => 'Doe'
);
$xml = '<?xml version="1.0" encoding="utf-8"?>
<?qbxml version="11.0"?>
<QBXML>
<QBXMLMsgsRq onError="stopOnError">
<CustomerAddRq>
<CustomerAdd>
<CompanyName>' . $arr['name'] . '</CompanyName>
<FirstName>' . $arr['fname'] . '</FirstName>
<LastName>' . $arr['lname'] . '</LastName>
</CustomerAdd>
</CustomerAddRq>
</QBXMLMsgsRq>
</QBXML>';
return $xml;
}
function _quickbooks_customer_add_response($requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $xml, $idents)
{
global $theme_dir;
$data = json_decode(json_encode(simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA)), true);
file_put_contents($theme_dir . '/customer_response.txt', 'ListID = ' . mysql_escape_string($idents['ListID']) . PHP_EOL . var_export($data, true) . PHP_EOL . PHP_EOL . 'idents' . PHP_EOL . var_export($idents, true), FILE_APPEND | LOCK_EX);
}
我从 Quickbooks 得到的唯一错误如下:
0x80040400: QuickBooks found an error when parsing the provided XML
text stream.
您需要删除此行:
// parse error somewhere in here, need to investigate this!
require_once(trailingslashit(get_home_path()) . 'server.php');
你不能这样测试。 Web 连接器 应该调用 server.php。你不应该。
您可以通过 AJAX 进行排队,但 qbXML requests/server 组件的实际处理需要由 Web 连接器触发,而不是由您触发。
请注意如何排队的文档中的示例如何不调用任何服务器组件:
使用 Keith Palmer 的 类 for Quickbooks,位于此处:https://github.com/consolibyte/quickbooks-php
我现在收到从 Quickbooks Web 连接器应用程序返回的解析错误。
基本上,我正在执行一个 AJAX 请求,如下所示:
$('.form-item').on('submit', function(event) {
event.preventDefault();
var $this = $(this),
$index = $this.data('index'),
$company = $('input.company-' + $index).val(),
$status = $this.data('status');
// Current Action/Status being performed here!
console.log(current_action);
console.log($status);
var data = {
action: 'hunter_' + current_action,
security: HUNTER_admin[current_action + '_nonce'],
form: $this.serialize(), // serialize the form data
company: $company
};
// Might need to create Customer
if (current_action == 'create_estimate')
{
// This returns an array of current emails in the database and QB
data['qb_emails'] = HUNTER_admin.emails;
}
console.dir(data);
$.ajax({
type: 'POST',
url: HUNTER_admin.ajax_url,
data: data,
dataType: 'json'
}).fail(function(jqXHR, textStatus) {
alert("Request failed: " + textStatus);
}).done(function(response) {
alert('Success');
$('.' + $status + '-' + $index).replaceWith($('<div />').html(response.statustext));
}).always(function(response) {
console.dir(response);
});
现在我的 functions.php
wordpress 文件中的 ajax:
add_action('wp_ajax_hunter_create_estimate', 'hunter_create_estimate');
function hunter_create_estimate()
{
global $wpdb;
check_ajax_referer('create-estimate', 'security');
$response = array();
if (!current_user_can('manage_options'))
{
return $response;
die();
}
require_once(get_stylesheet_directory() . '/QuickBooks.php');
// Get All data!
parse_str($_POST['form'], $data);
// Check current emails in quickbooks and see if a match is found with users email, to determine if we need to create a New Customer in Quickbooks or not:
$qb_emails = !empty($_POST['qb_emails']) ? array_map('strtolower', $_POST['qb_emails']) : array();
if (empty($qb_emails) || !in_array(strtolower($data['email']), $qb_emails))
{
$data['has_email'] = false;
// Email not set within the database, so we need to add this customer to the quickbooks Queue!
$dsn = 'mysqli://' . DB_USER . ':' . DB_PASSWORD . '@' . DB_HOST . '/' . DB_NAME;
$Queue = new QuickBooks_WebConnector_Queue($dsn);
$Queue->enqueue(QUICKBOOKS_ADD_CUSTOMER);
}
else
$data['has_email'] = true;
// Now fire up the Queue that will Add the Estimate!
$response = array_merge($data, array('company' => $_POST['company'], 'statustext' => '<p class="text">Creating Estimate...</p>', 'emails' => $_POST['qb_emails']));
echo json_encode($response);
die();
}
一切正常,直到我连接 Quickbooks.php 文件并调用队列。现在我在 Quickbooks 的提交和响应期间遇到解析器错误。
实际上我只是想测试一个 QUICKBOOKS_ADD_CUSTOMER
调用:
我的地图数组如下所示:
$map = array(
QUICKBOOKS_ADD_CUSTOMER => array( '_quickbooks_customer_add_request', '_quickbooks_customer_add_response' ),
QUICKBOOKS_QUERY_CUSTOMER => array('_quickbooks_query_customer_request', '_quickbooks_query_customer_response'),
);
2 个函数如下所示:
function _quickbooks_customer_add_request($requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $version, $locale)
{
$arr = array(
'name' => 'Doe Enterprises',
'fname' => 'John',
'lname' => 'Doe'
);
$xml = '<?xml version="1.0" encoding="utf-8"?>
<?qbxml version="11.0"?>
<QBXML>
<QBXMLMsgsRq onError="stopOnError">
<CustomerAddRq>
<CustomerAdd>
<CompanyName>' . $arr['name'] . '</CompanyName>
<FirstName>' . $arr['fname'] . '</FirstName>
<LastName>' . $arr['lname'] . '</LastName>
</CustomerAdd>
</CustomerAddRq>
</QBXMLMsgsRq>
</QBXML>';
return $xml;
}
function _quickbooks_customer_add_response($requestID, $user, $action, $ID, $extra, &$err, $last_action_time, $last_actionident_time, $xml, $idents)
{
global $theme_dir;
$data = json_decode(json_encode(simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA)), true);
file_put_contents($theme_dir . '/customer_response.txt', 'ListID = ' . mysql_escape_string($idents['ListID']) . PHP_EOL . var_export($data, true) . PHP_EOL . PHP_EOL . 'idents' . PHP_EOL . var_export($idents, true), FILE_APPEND | LOCK_EX);
}
我从 Quickbooks 得到的唯一错误如下:
0x80040400: QuickBooks found an error when parsing the provided XML text stream.
您需要删除此行:
// parse error somewhere in here, need to investigate this! require_once(trailingslashit(get_home_path()) . 'server.php');
你不能这样测试。 Web 连接器 应该调用 server.php。你不应该。
您可以通过 AJAX 进行排队,但 qbXML requests/server 组件的实际处理需要由 Web 连接器触发,而不是由您触发。
请注意如何排队的文档中的示例如何不调用任何服务器组件: