Note that there are some explanatory texts on larger screens.

plurals
  1. POCakephp: Ajax-request goes to the action it's called from when another url is specified
    text
    copied!<p>I'm sorry for the unpresice title, but I'm having trouble describing my problem without showing some code.</p> <p>I have an action - edit_one - which is defined in routes.php like this:</p> <pre><code>Router::connect('/edit_one/:ad_id', array('controller' =&gt; 'ads', 'action' =&gt; 'edit_one')); </code></pre> <p>As you can see it takes a parameter - ad_id.</p> <p>I have an jQuery-ajax function in the edit_one view, like this:</p> <pre><code>$.ajax({ url: "delete_from_facebook", type: "POST", data: data, success: function (res) { console.log(res); } }); </code></pre> <p>The url 'delete_from_facebook' is defined in routes.php like this:</p> <pre><code>Router::connect('/delete_from_facebook', array('controller' =&gt; 'ads', 'action' =&gt; 'deleteFromFacebook')); </code></pre> <p>The deleteFromFacebook function in the Controller:</p> <pre><code>public function deleteFromFacebook() { $this-&gt;autorender = false; try { $this-&gt;log($this-&gt;request-&gt;data, 'debug'); $response = $this-&gt;fb_handler-&gt;deletePhotoFromFacebook($this-&gt;fb, $this-&gt;request-&gt;data['id']); $this-&gt;log($response, 'debug'); return new CakeResponse(array('body' =&gt; json_encode($response))); } catch (FacebookApiException $fbe) { $this-&gt;showFacebookAPIException($fbe); } catch (Exception $e) { $msg = __('An error has occured while uploading images. Please try again.'); $this-&gt;showException($e, $msg); } } </code></pre> <p>My problem is that the ajax-request will always post to the edit_one action, IF the url is relative. If the url provided is absolute it will post to deleteFromFacebook function. </p> <p>I know the reason for this. It's because the edit_one action takes a parameter. I've tried it without, then the ajax-request goes to the right place. I'm using the same ajax-request from the view to make an ad, which is defined in the routes.php like this:</p> <pre><code>Router::connect('/add', array('controller' =&gt; 'ads', 'action' =&gt; 'add')); </code></pre> <p>As you can see from the routes, add.ctp and edit_one.ctp is in same folder. A relative path should work just as well from both.</p> <p>I must have the parameter and I have to use a relative path. What can I do to get the ajax-request to post to the right place?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload