• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I grab the json object and ignore the rest?

#1
[eluser]jshultz[/eluser]
This is what is happening. I've got a form that lets me create or update a page. When it's creating a page the pageid value is 0. Otherwise, pageid is the value of whatever the page is I'm editing. When I hit submit, a jquery function is listening for the event and serializes the data and sends it off to the controller. The controller hands it off to the model. In the model, depending on whether or not it's a create or update, will determine whether or not a json object is echoed back. Either way, the controller updates the routes file irregardless, because the form could be creating or updating the page. That means, the rank of the page could be updated or a page could be moved to be a child of another page, etc.

The success part of my ajax call is listening to see what data is returned. Specifically, i'm looking for the new pageid of the newly created page. I'll use that to add the list item to the sidebar where the pages are listed. The problem is, not only is the json object getting returned, but an array containing all the pages is getting returned because of the routes file being updated.

Is there a way I can "listen" or grab just the json object and ignore everything else?

Here's a sample of the response I get after a page creation according to firebug:

Code:
[{"pageid":"13","page_name":"test apge 43","page_headline":"New Page Headline Goes Here","page_intro":"","page_content":"<p>\n Insert Page Content Here<\/p>","parentid":"0","templateid":"0","sectionid":"0","userid":"1","siteid":"4556602514ec4a0885a5fa","rank":"0"}]

array(10) {
  ["pageid"]=>
  string(1) "1"
  ["page_name"]=>
  string(5) "index"
  ["page_headline"]=>
  string(18) "Welcome to My Page"
  ["page_intro"]=>
  string(0) ""
  ["page_content"]=>
  string(38) "<p>
Here's some page content.</p>"
  ["parentid"]=>
  string(1) "0"
  ["sectionid"]=>
  string(1) "1"
  ["userid"]=>
  string(1) "1"
  ["siteid"]=>
  string(22) "4556602514ec4a0885a5fa"
  ["rank"]=>
  string(1) "0"
}
array(10) {
  ["pageid"]=>
  string(2) "12"
  ["page_name"]=>
  string(12) "test page 42"
  ["page_headline"]=>
  string(27) "New Page Headline Goes Here"
  ["page_intro"]=>
  string(0) ""
  ["page_content"]=>
  string(33) "<p>
Insert Page Content Here</p>"
  ["parentid"]=>
  string(1) "0"
  ["sectionid"]=>
  string(1) "0"
  ["userid"]=>
  string(1) "1"
  ["siteid"]=>
  string(22) "4556602514ec4a0885a5fa"
  ["rank"]=>
  string(1) "0"
}
array(10) {
  ["pageid"]=>
  string(2) "13"
  ["page_name"]=>
  string(12) "test apge 43"
  ["page_headline"]=>
  string(27) "New Page Headline Goes Here"
  ["page_intro"]=>
  string(0) ""
  ["page_content"]=>
  string(33) "<p>
Insert Page Content Here</p>"
  ["parentid"]=>
  string(1) "0"
  ["sectionid"]=>
  string(1) "0"
  ["userid"]=>
  string(1) "1"
  ["siteid"]=>
  string(22) "4556602514ec4a0885a5fa"
  ["rank"]=>
  string(1) "0"
}
array(10) {
  ["pageid"]=>
  string(1) "4"
  ["page_name"]=>
  string(11) "subpagetest"
  ["page_headline"]=>
  string(13) "Sub Page test"
  ["page_intro"]=>
  string(0) ""
  ["page_content"]=>
  string(65) "<p>
Example data goes here. And it doesn't go here. duh.</p>"
  ["parentid"]=>
  string(1) "0"
  ["sectionid"]=>
  string(1) "1"
  ["userid"]=>
  string(1) "1"
  ["siteid"]=>
  string(22) "4556602514ec4a0885a5fa"
  ["rank"]=>
  string(1) "1"
}
array(10) {
  ["pageid"]=>
  string(1) "5"
  ["page_name"]=>
  string(10) "Contact Us"
  ["page_headline"]=>
  string(10) "Contact Us"
  ["page_intro"]=>
  string(0) ""
  ["page_content"]=>
  string(69) "<p>
This would be a great contact us page. It's pretty cool.</p>"
  ["parentid"]=>
  string(1) "0"
  ["sectionid"]=>
  string(1) "0"
  ["userid"]=>
  string(1) "1"
  ["siteid"]=>
  string(22) "4556602514ec4a0885a5fa"
  ["rank"]=>
  string(1) "2"
}

#2
[eluser]john_j[/eluser]
i think the php function called json_decode($your_string) should help you .

#3
[eluser]jshultz[/eluser]
True. but I want to grab/process the json object that comes first and ignore the array that follows it.

#4
[eluser]jshultz[/eluser]
Ok, i solved the problem. I added a new ajax call inside the first success method that would update the routes file. This keeps the second array from being reported back after the json object is returned.

#5
[eluser]Aken[/eluser]
I don't understand why all of this data is output in the first place. Why is the controller "returning" multiple things? The array just looks like a var_dump anyway, probably shouldn't even be there. Why don't you split up your logic to have one controller focus on creating, and another on editing? They can share a common private function for most of the work, but then output the appropriate data for that method.

#6
[eluser]jshultz[/eluser]
Ok, this is sad. it was from a var_dump. It was hidden in my save_routes function and never got removed. How dumb. Sad I had looked for something like an echo or var_dump but didn't see it. I'm going to do a search through my app folder for any more var_dumps now.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.