Note that there are some explanatory texts on larger screens.

plurals
  1. POAjax reads success as error from zend rest controller
    primarykey
    data
    text
    <p>I'm using ajax to do a POST request to an API built on the Zend Framework. The API is returning a success JSON message, but AJAX is reading it as an error.</p> <p>I've read through about a half dozen posts and implement their solutions. Nothing is helping.</p> <p>My Zend Controller from the API</p> <pre><code>class LeadController extends Zend_Rest_Controller { public function init() { $this-&gt;_helper-&gt;viewRenderer-&gt;setNoRender(true); } public function postAction() { $this-&gt;getResponse() -&gt;setHttpResponseCode(201) -&gt;setHeader('Cache-Control','no-cache', true) -&gt;setHeader('Content-Type', 'application/json', TRUE) -&gt;setHeader('charset', 'utf-8', TRUE) -&gt;appendBody(json_encode(array('results' =&gt; 'success'))); return; } } </code></pre> <p>I'm using Charles to verify that the Response code and content types are being set correctly.</p> <p>Here's my JSON response, which I've validated.</p> <pre><code> {"results":"success"} </code></pre> <p>My AJAX code</p> <pre><code>$.ajax({ type: 'POST', url: myurl, data: inputs, cache: 'false', success: function(data){ console.log('success'); }, error: function(xhr, status, thrown){ console.log("Error:"); console.log(status); } }); </code></pre> <p>The success function never gets called. It's always the error function. What am I doing wrong?</p> <p>UPDATE</p> <p>I tried it out in Chrome and got an xmlhttprequest cannot load access-control-allow-origin error message. After some investigation I found that you can't use AJAX to access a URL on a different domain. I created a PHP script on the same domain as the Javascript that calls the API via curl. I've set my ajax script up to use the go-between script and it's working just fine.</p> <p>I just changed the url line in the ajax function to get it working</p> <pre><code>url: 'localscript.php', </code></pre> <p>Here's the localscript.php file that is calling the Zend Rest API in case anyone finds it useful.</p> <pre><code>if ($_SERVER['REQUEST_METHOD'] === 'POST') { //add fields from POST foreach($_POST as $key =&gt; $value) { $data[$key] = $value; } //url-ify the data for the POST $fields_string =''; foreach($data as $key=&gt;$value) { $fields_string .= $key.'='.urlencode($value).'&amp;'; } rtrim($fields_string,'&amp;'); //open connection $ch = curl_init(); //set the url, number of POST vars, POST data curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string); curl_setopt($ch,CURLOPT_POST,true); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); curl_setopt($ch,CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)'); //execute post $result = curl_exec($ch); $info = curl_getinfo($ch); $start = stripos($result, "&lt;body"); $end = stripos($result, "&lt;/body"); $body = substr($result,$start,$end); echo $body; //close connection curl_close($ch); </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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