0

I am looking for a way to put variables in to a AJAX get call, now i know the obvious way to do it would just be to add it too "data" like so

$.ajax({
            type: "get",
            url: "api.php",
            data: {sessionkey: sessionkey, request: 'createapplication', favourid: favourid, userid: userid, message:message },
            success: function(data) {

                console.log(data);
           }

       });

But this goes to an api and the api also handles request from an iOS app which put the data into httpBody like so

 let json: [String: Any] = ["userid":userID, "message":applicationtext.text, "favourid":selectedFavour]

        let jsondatatosend = try? JSONSerialization.data(withJSONObject: json)

        // create post request

        let url = "myurl";
        var request = URLRequest(url: url)
        request.httpMethod = "POST"

        // insert json data to the request
        request.httpBody = jsondatatosend

I believe the reason i did this origionally was it was messing up because of having strange characters in the URL so i had to send it through the body which all worked well, but now im trying to get a website to follow the same method on my api i would like it to be sent in the body from ajax so my php can do this function

$inputJSON = file_get_contents('php://input');
$input = json_decode($inputJSON, TRUE);

I understand there are many ways for me to get around it in my php just use $_GET[' var '] instead of file_get_contents when it is sent from the AJAX of my website but i was wondering if there was a way of sending it into the body via ajax so i dont have to change the php file and then it is not sent through url's

so what i want to be able to do is something like this

    $.ajax({
                type: "get",
                url: "api.php",
                data: {sessionkey: sessionkey, request: 'createapplication'}, 
                httpBody: {favourid: favourid, userid: userid, message:message },
                success: function(data) {

                    console.log(data);
               }

           });
Ray
  • 123
  • 11
  • The iOS "app" uses `POST`, correct? – guest271314 Feb 09 '19 at 11:38
  • yes but ive tried both theres not really any difference for the purpose of what im trying to do as far as i know – Ray Feb 09 '19 at 11:55
  • Why do you not `POST` the `JSON`? – guest271314 Feb 09 '19 at 11:55
  • this still doesnt make it availiable from the php call "$inputJSON = file_get_contents('php://input');" does it? ive tried it and sent back the results for that $input and it is nothing – Ray Feb 09 '19 at 11:57
  • You can use `fetch()` to `POST` the `JSON` as `Blob` and use `php://input`, see https://stackoverflow.com/questions/37491759/ – guest271314 Feb 09 '19 at 12:00
  • would you be able to give me an example using my code? – Ray Feb 09 '19 at 12:05
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/188141/discussion-between-ray-and-guest271314). – Ray Feb 09 '19 at 12:08

0 Answers0