Prevent php conversion $ _REQUEST json

Does anyone know of a way to keep PHP $ _REQUEST from automatically converting JSON? I am sending data via POST and want to save it in its original form and just store it as a string.

Ideally, I want a simple way to โ€œavoidโ€ a line segment. There seems to be probably an easy way to do this, but I haven't seen it.


Wow ... Please forgive my uncertainty about the wording of this question, I programmed a lot for several days and was just tired. I did not expect a rush to close my question before I could explain this misunderstanding.

I meant that I pass javascript objects to jQuery $ .ajax () as data and using type: "POST" to call. Later, I realized that $ .param () is used internally to serialize the object as a query string to pass, and that $ _REQUEST [] converts the objects passed in this way into array structures, as it would with any data of complex shape.


Client code

$.ajax({ type: "POST", url: "some.php", data: {"obj":{"key":"val"},"str":"text"} }) 
+4
source share
2 answers

When you execute a jQuery AJAX query, key / value pairs inside the data are sent as POST NOT parameters as JSON. If you want to publish the data as JSON, then you need to do something similar to the following:

 <script type="text/javascript" src="https://raw.github.com/gist/754454/c6401b8cc461f84799503484a0c780a7622b164d/jQuery.stringify.js"><!-- JSON library --></script> <script type="text/javascript"> var data = JSON.stringify({"obj":{"key":"val"},"str":"text"}}); $.ajax({'type':"POST", 'url':"some.php", 'data':{'data':data}); </script> 

Please note that we take what you usually passed as the argument to โ€œdataโ€, but instead we create a post parameter called โ€œdataโ€ with the value as a gated version of the data structure. Also known as JSON.

+5
source

In this particular case, I wanted to be able to do something like the following:

 $.ajax({ type: "POST", url: "some.php", data: { "obj": { "key": "val", "keepStr": JSON.stringify({ "innerObj" { "item1": "val", "item2": "val2" } }) }, "str": "text" } }); 

and save "innerObj" directly in db as a JSON string. As already mentioned, I was under the (erroneous) impression that $ .ajax () converts data to JSON for transmission. As explained by another answer, this is not the case, but instead uses $ .param () to convert.

Thanks for the answers and for reopening the question.

+1
source

All Articles