0

我正在使用 cakePHP 1.26。

我有一个包含 URL 的输入文本框,我想提交 URL 并使用 Jquery AJAX 将其存储在数据库中。

这是 HTML 部分:

<input type="text" id="testing" value="https://stackoverflow.com/questions/ask">

这是 JQuery 部分:

  var whatContent=$("#testing").val();
      var curl="http://localhost:8080/test/grab/"+whatContent;
      $.ajax({
      type: "POST",
      url: curl,   
      success: function(data) {    
      alert(data);}
      });

这是控制器中动作的代码:

function grab($w=null){
   if($w!=null){
     return $w;
    }
}

代码有效,我可以看到弹出警报消息,但消息中缺少某些内容。我的意思是我应该看到这样的整个 URL:

https://stackoverflow.com/questions/ask

但不是,我只是看到了其中的一部分:

http://stackoverflow.com

后来我改变了输入文本框中的值,如下所示:

<input type="text" id="testing" value="https://stackoverflow.com/faq">

但是同样,返回的值仍然是

http://stackoverflow.com

cakePHP 似乎将 URL 视为一些参数而不是 URL。

请帮忙

4

1 回答 1

1

当您像您一样将内容附加到“curl”变量的末尾时,您正在尝试将其添加到通过GET变量检索并会在请求中获得结果,例如http://localhost:8080/test/grab/http://stackoverflow.com/questions/ask. 显然这是一个无效的请求。您的GET变量解析不会是一致的,并且是一种将数据传回控制器的危险方式(特别是如果用户能够编辑附加值)。

相反,您应该使用datajQuery 中的属性将此信息传递回您的 POST 请求中,如以下说明中所述:http: //api.jquery.com/jQuery.ajax/

在 Cake 方面,您将能够以$this->data['IDValueYouConfigured']. 例如,如果您的 AJAX 请求是这样的:

  var whatContent=$("#testing").val();
  var curl="http://localhost:8080/test/grab/";
  $.ajax({
  type: "POST",
  url: curl,
  data: "formValue="+whatContent,   
  success: function(data) {    
  alert(data);}
  });

我之前提到formValue的那个在哪里。IDValueYouConfigured

More importantly, you seem to be misunderstanding proper use of the Cake framework and could be performing all of these functions MUCH more simply using things like the JsHelper, FormHelper, etc. I would recommend using the most RECENT version of Cake (1.3.3) and follow through the Blog tutorial at least once. This will lead to better questions which will be more likely to get helpful answers. Hope this helps.

于 2010-08-01T13:59:06.110 回答