简体   繁体   中英

Passing a PHP object via AJAX?

I am making an AJAX request and I need to send a complete PHP object. This is my JS code:

new Ajax.Request("some url", 
    {
        method: 'post',
        parameters: {
                      string1:"whatever",
                      myobject: this is where I want to send a PHP object named $product
                    }

     ...
    });

Whats the best way? Maybe using serialization?

Thanks for your help :)!

您可以尝试使用json_encode(),通过另一端的请求和json_decode发送

It is not a good idea to send a plain php object. Even when you do something like json encoding. The problem is that the receiving side might not know about the objects class definition.

Instead I suggest you add some export/import moethods to your class definition. You send the (serialized) result of the export method and import that (unserialized) on the receiving side. That way it is a) clear that you have a valid class on the receiving side and b) you have a place to handle missmatches. You are more flexible and safer.

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