【问题标题】:Yii Async jsonp requestYii 异步 jsonp 请求
【发布时间】:2011-11-27 02:07:23
【问题描述】:

我是 Yii PHP 框架的新手,所以请耐心等待。

我需要发出一个跨域 JSONP 请求(来自非 yii 应用程序)以在 Yii 应用程序数据库中创建记录。创建后应通过 getVisit 返回 Application/json 内容

控制器:

public function actionGetVisit($id)
{
  header('Content-type: application/json');

  $visit = Visit::model()->findByPK((int)$id);

  echo CJSON::encode($visit);

  Yii::app()->end();
}
/**
 * Creates a new model.
 * If creation is successful, the browser will be redirected to the 'view' page.
 */
public function actionCreate()
{
    $model=new Visit;

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['Visit']))
    {
        $model->attributes=$_POST['Visit'];
        if($model->save())
            $this->redirect(array('getVisit','id'=>$model->id));
    }

    $this->render('create',array(
        'model'=>$model,
    ));
}

形式:

<form id="visit-form" action="http://host/index.php?r=visit/create" method="post">
            <p class="note">Fields with <span class="required">*</span> are required.</p>


            <div class="row">
                <label for="Visit_rvc_id" class="required">Rvc <span class="required">*</span></label>      <input name="Visit[rvc_id]" id="Visit_rvc_id" type="text" value="1">            </div>

            <div class="row">
                <label for="Visit_zone" class="required">Zone <span class="required">*</span></label>       <input name="Visit[zone]" id="Visit_zone" type="text" value="1">            </div>

            <div class="row">
                <label for="Visit_table" class="required">Table <span class="required">*</span></label>     <input name="Visit[table]" id="Visit_table" type="text" value="1">          </div>

            <div class="row">
                <label for="Visit_seat" class="required">Seat <span class="required">*</span></label>       <input name="Visit[seat]" id="Visit_seat" type="text" value="1">            </div>

            <div class="row">
                <label for="Visit_user_id" class="required">User <span class="required">*</span></label>        <input name="Visit[user_id]" id="Visit_user_id" type="text" value="1">          </div>

            <div class="row">
                <label for="Visit_guest_name" class="required">Guest Name <span class="required">*</span></label>       <input size="60" maxlength="256" name="Visit[guest_name]" id="Visit_guest_name" type="text"  value="1">         </div>

            <div class="row">
                <label for="Visit_created" class="required">Created <span class="required">*</span></label>     <input name="Visit[created]" id="Visit_created" type="text" value="1">          </div>

            <div class="row buttons">
                <input type="submit" name="yt0" value="Create"> </div>

        </form>

JS:

$('#visit-form').submit(function(event)
        {
            alert('submit');
            event.preventDefault();
            var $form = $(this);
            $.ajax({
                url: $(this).attr('action'),
                dataType: 'jsonp',
                type: 'POST',
                data : $form.serialize()+'&ajax='+$form.attr('id'),
                success: function(data, textStatus, XMLHttpRequest)
                {
                    alert('success');
                    if (data != null && typeof data == 'object'){
                        $.each(data, function(key, value){
                            $('#error').append(value);
                        });
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown)
                {
                        alert(errorThrown);
                        alert('error');
                }
            });
            return false;
        });

提交后: 看起来它没有出错或成功。回复说:

GET http://host/index.php?r=visit/create&callback=jQuery15102636089683510363_1317230765087&Visit%5Brvc_id%5D=1&Visit%5Bzone%5D=1&Visit%5Btable%5D=1&Visit%5Bseat%5D=1&Visit%5Buser_id%5D=1&Visit%5Bguest_name%5D=1&Visit%5Bcreated%5D=1&_=1317230785272 The URL can’t be shown

响应设置为输出文本/

有谁知道这个错误是什么意思?没有js,表单提交完美。但我似乎无法让 ajax 请求正常工作。我将其设置为“jsonp”,这样跨域问题就会消失。但我不确定 Yii 后端是否可以处理以 jsonp 形式发送的数据。任何帮助表示赞赏!

【问题讨论】:

    标签: jquery ajax jsonp yii


    【解决方案1】:

    这不是一个真正的 Yii 问题,更多的是 JSONP 问题;下面是您的 GetVisit 函数的外观:

    public function actionGetVisit($id)
    {
      header('Content-type: application/json');
    
      $visit = Visit::model()->findByPK((int)$id);
    
      $json = CJSON::encode($visit);
      echo $_GET['callback'] . ' (' . $json . ');';
    
      Yii::app()->end();
    }
    

    jQuery 将一个全局临时函数附加到在 JSONP 请求期间插入脚本时调用的窗口对象。 jQuery 替换了 ?使用调用内联函数的生成函数名称(即 jsonp1232617941775)。您正在将该函数传递给窗口对象。

    希望对您有所帮助,如果我在做一个项目时没有正确解释,请致歉。

    【讨论】:

      猜你喜欢
      • 2012-04-17
      • 2023-04-09
      • 1970-01-01
      • 2016-07-17
      • 2014-07-13
      • 2014-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多