0

I am new in Symfony, and trying to fix a bug. I am getting Access-Control-Allow-Origin header is present on the requested resource error while inserting data via restApi.

This is a restful api that is sending data in the given format:

{"default_runs":["24"],"date":1451932200,"driver":107}

Controller

public function postScheduleAction(Request $request)
{   
    $user = $this->getUser();
    $user_id = $user->getId();
    $entity = new Schedule();
    $form = $this->createForm(new ScheduleType(), $entity);
    $now = time();

    $data = json_decode($request->getContent(), true);
    $data["is_default"] = true;
    $data["status"] = 0;            

    $finalData = array_merge($data,array("created_time"=>$now,"created_by"=>$user_id));
    $request->request->replace(is_array($finalData) ? $finalData : array());

    //I have checked, code is working fine till this line,
    //but after bind the form it gives me error

    $form->bind($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();

        return array(
            'id' => $entity->getId(),
        );
    }

    return array(
        'form' => $form,
    );

}

Conclusion after debugging

I have searched a lot, dump on every line, at the last I have change the insert function of symfony with custom php-msql insert query then it works fine.

So now I know that there is any error with the $form->bind($request); line, it through any fetal error that missed the Access-Control-Allow-Origin header and I get that error.

Can some one please help me to catch the error in $form->bind($request);, so that I can fix that error.

Thanks

Anshul Mishra
  • 1,706
  • 2
  • 15
  • 38
  • Start by pressing F12 on your browser and checking to see what headers are actually being sent. The compare the results with a working request. That should narrow things down. Also, messing around with the request object is not something that you should normally need to do but it's probably not causing the problem. – Cerad Jan 05 '16 at 13:09
  • I have checked that many time, the same function is working for update the row in first if condition, but when it goes in else condition (for insert) it through this error after reading `$form->bind($request);` line. – Anshul Mishra Jan 05 '16 at 14:00
  • I have debug the code by inserting return statement after each line, that statement works above the `$form->bind($request);` but not after that. I think there is not catch statement to catch this error that is why it return the error in wrong format that occur the error. The header is missing in `Response Header` (Access-Control-Allow-Origin:*) while error, otherwise it shows that error – Anshul Mishra Jan 05 '16 at 14:04
  • I have checked a lot in code, I just find that error is in `$form->bind($request);` this line, but I am unable to catch the error, is there any way by that I can get the error? – Anshul Mishra Jan 07 '16 at 08:18

3 Answers3

0

I think that your error is different and is says:

"No Access-Control-Allow-Origin header is present on the requested resource"

and the error appears because you want to make AJAX request from other domain that server stands on.

It is connected with Same Origin Policy and you probably need to implement CORS

Check also these links:

Community
  • 1
  • 1
Robert
  • 19,800
  • 5
  • 55
  • 85
  • I think the actual issue is not related to that, because all the other functions are working fine, and it is using `"friendsofsymfony/rest-bundle": "~1.1",` for rest api and that is handling the `CORS` – Anshul Mishra Jan 05 '16 at 13:02
0
Access-Control-Allow-Origin: *

Must be in the header of the response of the RESTful api, Or it needs to be withing your domain otherwise it is Cross-Origin Resource Sharing (which most sites have not allowed).

You can still proxy an external resource on a server within your domain (and even include the header) then hit it with your controller.

S.Pote
  • 71
  • 1
  • 5
0

I have find the issue and the problem has been solved, thanks to all for their answers.

Actually the problem was not with Access-Control-Allow-Origin header (CORS). There was an issue in database foreign key relation. There was a wrong entry in a child table that was not related with the parent table.

So while binding the form $form->bind($request); it was checking the foreign key relation and that fails because of that wrong entry, and it return an error that was not formatted (with Access-Control-Allow-Origin header). That is why it was showing me the Access-Control-Allow-Origin header is present on the requested resource error.

Anshul Mishra
  • 1,706
  • 2
  • 15
  • 38