<p>100% untested, but try this:</p>

<pre><code>return [
    'endpoints' =&gt; [
        'api/jobs.json' =&gt; function() {
            HeaderHelper::setHeader([
                'Access-Control-Allow-Origin' =&gt; 'http://example.com'
            ]);

            return [
                'elementType' =&gt; 'Entry',
                'criteria' =&gt; ['section' =&gt; 'jobs'],
                'transformer' =&gt; function(EntryModel $entry) {
                    return [
                        'title' =&gt; $entry-&gt;title,
                        'url' =&gt; $entry-&gt;url,
                        'jsonUrl' =&gt; UrlHelper::getUrl("jobs/{$entry-&gt;id}.json"),
                    ];
                },
            ];
        },
        'api/jobs/&lt;entryId:\d+&gt;.json' =&gt; function($entryId) {
            HeaderHelper::setHeader([
                'Access-Control-Allow-Origin' =&gt; 'http://example.com'
            ]);

            return [
                'elementType' =&gt; 'Entry',
                'criteria' =&gt; ['id' =&gt; $entryId],
                'first' =&gt; true,
                'transformer' =&gt; function(EntryModel $entry) {
                    return [
                        'title' =&gt; $entry-&gt;title,
                        'url' =&gt; $entry-&gt;url,
                        'entryId' =&gt; $entry-&gt;id,
                        'postDate' =&gt; $entry-&gt;postDate,
                    ];
                },
            ];
        },
    ]
];
</code></pre>

CC BY-SA 3.0
2016-12-28T19:34:21.353Z
2016-12-28T19:34:21.353Z
Brad Bell
<p>I'm running into this effectively <a href="https://github.com/pixelandtonic/ElementAPI/issues/4" rel="nofollow noreferrer">https://github.com/pixelandtonic/ElementAPI/issues/4</a> and I'm not quite sure how I'd integrate this change with <a href="https://gist.github.com/stevieg83/85dd058dc77a3b46764165975b17bb76" rel="nofollow noreferrer">https://gist.github.com/stevieg83/85dd058dc77a3b46764165975b17bb76</a> adding it as the first part of my function errors out for me </p>

<p><code>syntax error, unexpected '=&gt;' (T_DOUBLE_ARROW)</code></p>

<p>What's missing?</p>

<plugin-elementapi>
Element API CORS issue
# Element API CORS issue

(Asked by: username_0 on 2016-12-28)

I'm running into this effectively <https://github.com/pixelandtonic/ElementAPI/issues/4> and I'm not quite sure how I'd integrate this change with <https://gist.github.com/stevieg83/85dd058dc77a3b46764165975b17bb76> adding it as the first part of my function errors out for me

`syntax error, unexpected '=>' (T_DOUBLE_ARROW)`

What's missing?

**Tags:** plugin-elementapi


---

## 1 answers

### Answer by username_1 on 2016-12-28. Score: 2 | Accepted answer

100% untested, but try this:
return [
    'endpoints' => [
        'api/jobs.json' => function() {
            HeaderHelper::setHeader([
                'Access-Control-Allow-Origin' => 'http://example.com'
            ]);

            return [
                'elementType' => 'Entry',
                'criteria' => ['section' => 'jobs'],
                'transformer' => function(EntryModel $entry) {
                    return [
                        'title' => $entry->title,
                        'url' => $entry->url,
                        'jsonUrl' => UrlHelper::getUrl("jobs/{$entry->id}.json"),
                    ];
                },
            ];
        },
        'api/jobs/.json' => function($entryId) {
            HeaderHelper::setHeader([
                'Access-Control-Allow-Origin' => 'http://example.com'
            ]);

            return [
                'elementType' => 'Entry',
                'criteria' => ['id' => $entryId],
                'first' => true,
                'transformer' => function(EntryModel $entry) {
                    return [
                        'title' => $entry->title,
                        'url' => $entry->url,
                        'entryId' => $entry->id,
                        'postDate' => $entry->postDate,
                    ];
                },
            ];
        },
    ]
];


---

