0

I have a php application that accesses Asana API. I am able to create a project in Asana. However, the ajax call to the API class is returning a readystate=0.

While troubleshooting in firebug I also noticed that the network console has a 302, 400(??), and 200 status code. I thought 400 status code is related to invalid request or malformed url, but the project gets created anyway.

Any idea?

Update: More information.

I have a Ajax call to a php file which intern calls Asana API to getAuth code and tokens before calling the API services. I believe I am getting the CORS warning and hence the readystate=0 and the 400 error. However because rest of my script proceeds with the token it was inserting records anyways. However, after the tokens expired (3600 sec), now I am unable to insert records. I know if I call the php file directly it works without the CORS error.

$.ajax({
                    type: 'POST',
                    url: "oa/asana.php",
                    data: {apiprovider:"asana",type:"addnewproject",notes:"notes",name:"name",id:"id",resource:"projects"},
                    //dataType:"json",
                    //contentType: "application/json",
                    success: function(data) {

                            console.log(data);
                    },
                    error: function( error )
                            {
                              console.log("asana api error");
                              console.log(JSON.stringify(error)) ;
                    },
                    async:true
            });

my php code looks like this.

...$asana = new AsanaAuth(API_KEY,API_SECRET,callbackUrl);

    if (!isset($_GET['code'])) {
            $url = $asana->getAuthorizeUrl();
            header('Location:' . $url);
    } else {
            $asana->getAccessToken($_GET['code']);
             if ($asana->hasError()) {
                    echo 'Error response code: ' . $asana->responseCode;
            }
            else {
                    echo $asana->response;
            }
    }

Is there a better way to do this outside of Ajax call?

1 Answers1

0

OK here's how I fixed it. Partly my understanding was wrong and I made a wrong assumption.

This is purely based on my application's need and this may not be applicable to all.

I have a settings page where the user clicks on the Asana app to authorize the connection. This is not a Ajax call but a hyperlink to a file which is also my redirect uri. Within this file I check if the user has already authorized if not I call authorize, get tokens and store the refresh token in the database for future use.

In the other parts of my application, when the user clicks on create an asana project, I check for the validity of the access token if it has expired, I refresh and get another token again.

Earlier, I was trying to call this from one page (which in a normal user flow will never happen -in my application). So right now there is no Ajax call for authorize but calling the refresh token, and Asana API endpoints are through Ajax and they work fine.

Hope this helps someone in future.