I am trying to make simple ajax :
我正在尝试制作简单的ajax:
$('.send-contact-button').on('click', function(e){
e.preventDefault();
var id = $('.send-contact-button').data('id');
var flag = $('.send-contact-button').data('flag');
$.ajax({
method : 'GET',
url : "contactHandler?id="+id+"&flag="+flag,
success : function( data ){
console.log( data );
}
});
});
but every time i get the html content. Tryied to send the data through json but without result. This Ajax is just test. Controller:
但是每次我得到html内容。Tryied通过json发送数据,但没有结果。这个Ajax只是测试。控制器:
public function contactHandler($id, $flag)
{
echo $id;
}
Can you guys tell me where i am wrong ? Thank you very much!
你们能告诉我哪里不对吗?非常感谢!
2 个解决方案
#1
1
You ajax call should be formatted using yii2 urlmanager rules (contact-handler
instead of contactHandler
ajax调用应该使用yii2 urlmanager规则(contact-handler而不是contactHandler)进行格式化
$.ajax({
method : 'GET',
url : "contact-handler?id="+id+"&flag="+flag,
success : function( data ){
console.log( data );
}
});
and in your controller you must add the action text to your function name
eg:public function actionContactHandler($id, $flag)
在您的控制器中,您必须将动作文本添加到函数名eg:public function actionContactHandler($id, $flag)
#2
1
Your action is not defined correctly, you are probably getting an error page.
您的操作没有正确定义,您可能会得到一个错误页面。
try this:
试试这个:
public function actionContactHandler($id, $flag)
{
echo $id;
}
#1
1
You ajax call should be formatted using yii2 urlmanager rules (contact-handler
instead of contactHandler
ajax调用应该使用yii2 urlmanager规则(contact-handler而不是contactHandler)进行格式化
$.ajax({
method : 'GET',
url : "contact-handler?id="+id+"&flag="+flag,
success : function( data ){
console.log( data );
}
});
and in your controller you must add the action text to your function name
eg:public function actionContactHandler($id, $flag)
在您的控制器中,您必须将动作文本添加到函数名eg:public function actionContactHandler($id, $flag)
#2
1
Your action is not defined correctly, you are probably getting an error page.
您的操作没有正确定义,您可能会得到一个错误页面。
try this:
试试这个:
public function actionContactHandler($id, $flag)
{
echo $id;
}