[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 464
  • Last Modified:

PHP Post Request JSON

Hi all,

I need some example code to be able to get some info from an api.

This is what I've got from the api:

Method is:

http://www.mydomain.com/api/set/

and

http://www.mydomain.com/api/get/

Parameters/ Example Request

{
    'site': 'mysite',
    'id': 'some_id',
    'stage': 2,
}

and for the get

{
    'site': 'mysite',
    'id': 'some_id',
}

example resplonses would be:

for set:

{"status": "successful", "data": {}}

and for get:

{"status": "successful", "data": {stages: [1,1,0,1]}}


I just need some client code to get and set the information from the api ...

Hope all this info helps as it's all I have.

Thanks

0
error77
Asked:
error77
  • 7
  • 3
  • 2
1 Solution
 
Gurvinder Pal SinghCommented:
check jquery ajax
http://viralpatel.net/blogs/2009/04/jquery-ajax-tutorial-example-ajax-jquery-development.html
http://api.jquery.com/jQuery.ajax/

for example, for get

$.get(
    "http://www.mydomain.com/api/get/",
    "{
        'site': 'mysite',
        'id': 'some_id',
    }",
    function(data) { alert(eval(data).status); }
);


Similarly for set
0
 
error77Author Commented:
OK, I've done the above and to call the function added this:

<input type="button" value="Click Here" onClick="data();">

but nothing happens ... anything I'm doing wrong?

Thanks

0
 
Gurvinder Pal SinghCommented:
what is data() ??

and what exactly do you want to do with the data?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
error77Author Commented:
I want to go to that url and have some data returned ... for new an alert message with the return will do.

0
 
error77Author Commented:
In other word...post a request and get some data returned so I can then use them.
0
 
error77Author Commented:
As for your question;  What is data ...

I got it from your code:

$.get(
    "http://www.mydomain.com/api/get/",
    "{
        'site': 'mysite',
        'id': 'some_id',
    }",
    function(data) { alert(eval(data).status); }
);

function(data)  <-- so I guess the function was called data?
0
 
Gurvinder Pal SinghCommented:
no this function is not data.
In fact this is an anonymous callback handler

You need to put your get handler in data method to call it that way

function data()
{
 $.get(
    "http://www.mydomain.com/api/get/",
    "{
        'site': 'mysite',
        'id': 'some_id',
    }",
    function(data) { alert(eval(data).status); }
);
   
}
0
 
error77Author Commented:
OK, tried that but nothing is popup up .. how do I get a popup out this with the return please?

0
 
Ray PaseurCommented:
What is the actual URL of the web service?  Please post some test data so we can test the code to access the service.  Thanks.
0
 
error77Author Commented:
Sorry, I can't do that :o/
0
 
Ray PaseurCommented:
Well, if you can't post the actual URL, at least take the time to set up a test case that works the same way.  You're asking us for assistance - it would seem reasonable that you would give the fellow experts something more tangible than "please help me get my data right and btw, I will not let you see you my data."
0
 
error77Author Commented:
My mistype. It's now working fine thanks.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 7
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now