views:

98

answers:

4

Hi,

I am having a problem passing a json string back to a php script to process.

I have a json string that's been created by using dojo.toJson() that contains a / and looks like this:

[{"id":"2","company":"My Company / Corporation","jobrole":"Consultant","jobtitle":"System Integration Engineer"}]

When I pass the string back to the php script it get's chopped at the / and creates a malformed json string, which then means I can't convert it into a php array.

What is the best way of escaping the / in this string? I was looking at regular expressions and doing a string.replace() however my regex isn't that strong, and I'm not sure if there are better ways of doing this?

Many thanks

A: 

\/. Take a look here. The documentation is really easy to read, concise and clear. But unescaped / should still be valid in JSON's string so maybe your bug is somewhere else?

skalee
all I am doing at the moment is taking the string and passing it to the php script as a GET request and when stepping through with the debugger the string passed back stops at 'My Comany' with the rest missing.
Grant Collins
Ok. Kow EXACTLY you pass this variable to the PHP script (code)?BTW - passing JSON to PHP makes no sence. from PHP to JS - ok, but otherwise it's kind of stupid. JSON is basically Javascript Object so in PHP you have to encode it to PHP and then parse. Better pass a series of GET variables - PHP handles them natively.
Tomasz Struczyński
@Tomasz — PHP has handled JSON natively for a while (you just have to explicitly call the parsing function)
David Dorward
Yes I know, but this is not "natively", rather "has tools". json_encode/decode exists but... It's better to pass it via standard query, less parsing. In json var, first php library parses URL to 'distribute' it to $_GET vars, then you have to additionally encode it from JSON... it's easier to encode it correctly on JS side, if you have such option. Less work and less places it can go wrong.
Tomasz Struczyński
+3  A: 

You shouldn't need to do anything special to represent a / in JSON - a string can contain any character except a " or (when not used to start an escape sequence) \.

The problem is possibly therefore in:

  • the way you parse the JSON server side
  • the way your parse the HTTP data to get the JSON string
  • the way you encode the string before making the HTTP request

(I'd bet on it being the last of those options).

I would start by using a tool such as LiveHttpHeaders or Charles Proxy to see exactly what data is sent to the server.

(I'd also expand the question with the code you use to make the request, and the code you use to parse it at the other end).

David Dorward
HTTPFox is far more useful than LiveHTTPHeaders - it captures multiple requests and displays the components in a nice tabulated format, rather than LHH's text dump.
Marc B
A: 

Ok. Anyway.

When passing variables to PHP don't use JSON - it's good for passing variables other way.

Instead you better use http://api.dojotoolkit.org/jsdoc/1.3/dojo.objectToQuery method and on PHP side parse standard PHP $_GET variables.

EDIT: Ok, I'm 'lost in the woods' here also, but here's a tip - check if you don't have some mod_rewrite rules in action here. Kind of seems like that.

Also, if you can send me the URL which gave you 404 (you can cut out domain part, i'm interested in script filename and all afterwards) maybe I can give you more detailed answer.

Tomasz Struczyński
@Tomasz I've tried this but when looking at the call to the php script using firebug I get a 404 error, when I remove the / it goes through fine? There must be something else affecting this, that I am doing I will keep plugging at this.
Grant Collins
Also, if you can send me the URL which gave you 404 (you can cut out domain part, i'm interested in script filename and all afterwards) maybe I can give you more detailed answer.
Tomasz Struczyński
A: 

To be clear, whether you choose to send JSON to PHP or use regular form values is a matter of preference. It /should/ work either way. It sounds like you aren't url-encoding the JSON at the client-side so the server-side is treating / as a path delimiter. In which case its borked before json_decode gets to it.

so, try encodeURIComponent( dojo.toJson(stuff) )

Sam Foster