简体   繁体   中英

Passing string as parameter in Ajax

Lots of people seem to be having problems with passing parameters with ajax. I looked at many of those posts but none of the answers worked for me.

I am working in Symfony. I am trying to pass parameters by ajax so that i can update the table data, but even though javascript reads my data, i can't seem to be able to receive it in my controller.

The Javascript function is :

$('#form-save').click(function(){
    $('#myModal').modal('hide');
    var parent = $(this).parent().parent();
    var target = parent.attr('target');
    var title = $('#title-'+target);
    var id = $('#form-info').attr('row');
    var date = $('#date-'+target);
    var dateTime = new Date(date.text());
    var curr_date = ("0" + dateTime.getDate()).slice(-2);;
    var curr_month = ("0" + (dateTime.getMonth() + 1)).slice(-2);
    var curr_year = dateTime.getFullYear();
    var from = $('#from-'+target);
    var till = $('#till-'+target);
    var description = $('#description-'+target);
    //First Log
    console.log(target + title.text() + date.text() + from.text() + till.text() +description.text() );

    $.ajax({
        type: "POST",
        url: "{{ path('restaurant_my_events') }}",
        data: {
            action: 'update',
            id: id,
            title: title.text(),
            date: curr_year + "-" + curr_month + "-" + curr_date ,
            from: from.text(),
            till: till.text(),
            description: description.text()
        },
        dataType: "json",
        success: function(response) {
            //Second Log
            console.log( response );

        }
    });

For debugging i am logging twice on the console, the output from the first log is :

0 hello world   06-14-2014  12:0612:06 description 2 

This confirms that all the input elements are matched, there are no errors in reading the data, the output from the second log is just to verify that my parameters were received by my controller.

Controller Function is :

public function myEventsAction(Request $request){
    $restoID = 2;
    $repository = $this->getDoctrine()->getRepository('LesDataBundle:Party');
    $events = $repository->findByRestoId( $restoID );

    if ($request->isXMLHttpRequest()) {
        $status = false;
        $action = $request->get('action');
        $partyID = $request->get('id');
        if($action == 'delete'){
               //Delete function
        }else if($action == 'update'){
                //Update function
        }

        $test = $action ." + ". $partyID ." + ". $request->get('title') ." + " . $request->get('date') ." + " .
            $request->get('from') ." + " . $request->get('till') ." + " . $request->get('description');

        return new JsonResponse(array(
            'id' => $partyID , 'request' => $request , 'test' => $test));
    }


    return $this->render("LesRestaurantBundle:Admin:Events/myEvents.html.twig",
                array( 'events' => $events ));
}

And the output from the second logging is :

Object {id: "2", request: Object, test: "update + 2 +  + NaN-aN-aN +  +  + "}

I only received the parameter id. Why?

通过AJAX发送之前,请尝试使用encodeURIComponent()转换字符串。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM