jQuery 自动完成 mysql zend 操作/控制器没有响应?
我们在 zend 中使用 jquery 自动完成并设置我们的操作,如下所示
public function ajaxautocompleteAction()
{
$postData = $this->_request->getParams();
$term = $postData['term'];
$categoryObj = new Categories();
$result = $categoryObj->searchCategory($term);
$this->view->result = $result;
}
视图文件中的 javascript 是这样的
$(function() {
var url = "http://www.domain.com/account/ajaxautocomplete?format=json";
$( "#autotest" ).autocomplete({
minLength: 2,
source: function(request, response){
var iterm = request.term;
var url = "http://www.domain.com/account/ajaxautocomplete?format=json";
$.post( url, {term: iterm},
function( data ) {
response(data); });
}
});
});
在 chrome 控制台中我收到此错误
XMLHttpRequest 无法加载 http://www.domain.com/account/ajaxautocomplete?format=json。 Access-Control-Allow-Origin 不允许来源 http://domain.com。
有什么想法为什么没有从 ajax 请求中得到结果吗?
Were using jquery autocomplete in zend and setup our action like this
public function ajaxautocompleteAction()
{
$postData = $this->_request->getParams();
$term = $postData['term'];
$categoryObj = new Categories();
$result = $categoryObj->searchCategory($term);
$this->view->result = $result;
}
The javascript in the view file is this
$(function() {
var url = "http://www.domain.com/account/ajaxautocomplete?format=json";
$( "#autotest" ).autocomplete({
minLength: 2,
source: function(request, response){
var iterm = request.term;
var url = "http://www.domain.com/account/ajaxautocomplete?format=json";
$.post( url, {term: iterm},
function( data ) {
response(data); });
}
});
});
In chrome console i get this error
XMLHttpRequest cannot load http://www.domain.com/account/ajaxautocomplete?format=json. Origin http://domain.com is not allowed by Access-Control-Allow-Origin.
Any ideas why were not getting results from the ajax request?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是我之前使用 jQueryUI 的自动完成和 ZF 的方式...
创建您的操作
将 AjaxContext 添加到您的操作,禁用自动 JSON 序列化。我跳过自动序列化,因为您的模型并不常见来表示 jQueryUI 的自动完成功能查找的常见“标签”/“值”对
创建 JSON 视图 (
views/scripts/account/ajaxautocomplete.json .phtml
)<前><代码>结果为$category) {
// 格式化每个结果以实现 jQueryUI 自动完成
$数据[] = 数组(
'标签' => $category->getName(),
'值' => $类别->getName()
);
}
回声 Zend_Json::encode($data);
将自动完成操作的 URL 作为 JavaScript 变量添加到视图中需要使用它(假设您在布局中使用 HeadScript 助手)
像这样设置 JavaScript
This is how I've used jQueryUI's autocomplete and ZF before...
Create your action
Add an AjaxContext to your action, disabling automatic JSON serialisation. I'm skipping the auto serialisation as it's not common for your models to represent the usual "label" / "value" pairs jQueryUI's automcomplete looks for
Create your JSON view (
views/scripts/account/ajaxautocomplete.json.phtml
)Add the URL for your autocomplete action as a JavaScript variable to the view that needs to use it (assuming you use the HeadScript helper in your layout)
Setup your JavaScript like this
貌似domain.com不允许跨域调用。
尝试 chrome.exe --disable-web-security
looks like domain.com does not allow cross-domain calls.
Try
chrome.exe --disable-web-security