0
\$\begingroup\$

once again i need your help! with help from here i managed to make a function that sends a formated json text to a subscription service.the format is this

{
    "profiles":
    [
        {
            "email":"example@example.com"
        }
    ]
}

that works fine and great from my pc but once i upload the game it gets blocked by cors. Now i have cors enabled so i contacted klaviyo which handles the data and they told me their api does not accept data from the front end and i could use ajax to make it happer, an example of which is this:

var settings 
=
{

"async"
: true,

"crossDomain"
: true,

"url"
: 
"https://manage.kmail-lists.com/ajax/subscriptions/subscribe"
,

"method"
: 
"POST"
,

"headers"
: {

"content-type"
: 
"application/x-www-form-urlencoded"
,

"cache-control"
: 
"no-cache"

},

"data"
: {

"g"
: 
"LIST_ID"
,

"email"
: "email@address.com",

/
/
pass 
in 
additional fields (optional)

"$fields"
: 
"$source, $first_name, $last_name"
,

"$source"
: 
"Account Creation"
,

"$first_name"
: firstname,

"$last_name"
: lastname

}

};

$.ajax(settings).done(function(response) {

console.log(response);

})

now my code which is working from my pc is this :

public class testing : MonoBehaviour
{
    public InputField field;
    private string URL = "https://a.klaviyo.com/api/v2/list/YeXzKp/members?api_key=pk_ec448e1bdab7504c143466ca5eeabc5e95&profiles=[]";

    public void SaveData()
    {
        string data = JsonUtility.ToJson(
      new EmailApiData()
      {
          profiles = new EmailApiProfileData[]
          {
             new EmailApiProfileData() { email = field.text }
          }});
        StartCoroutine(SaveIntoJson(URL , data));
    }
    IEnumerator SaveIntoJson(string url, string data)
    {
        var request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbPOST);
        request.SetRequestHeader("Content-Type", "application/json");
        request.SetRequestHeader("Access-Control-Allow-Origin","*");
        request.SetRequestHeader("Access-Control-Allow-Credentials", "true");
        var jsonBytes = Encoding.UTF8.GetBytes(data);
        request.uploadHandler = new UploadHandlerRaw(jsonBytes);
        request.downloadHandler = new DownloadHandlerBuffer();
        yield return request.SendWebRequest();
        if (request.isNetworkError || request.isHttpError)
        {
            Debug.Log(request.error);
            Debug.Log(request.downloadHandler.text);
        }
        else
        {
            Debug.Log("Form upload complete!");
        }
        Debug.Log(data);
    }
}
[Serializable]
public class EmailApiProfileData
{
    public string email;
}
[Serializable]
public class EmailApiData
{
    public EmailApiProfileData[] profiles;
}

how can i put those two together?? i know im way out of my waters here but i want to learn it!Thank you once more!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.