1

I am looking to connect to a web service on a different domain in order to authenticate users. The Web Service itself is a RESTful service, written in Java. Data is passed to and from it in JSON.

I initially tried to connect using jQuery (see below)

        function Login()
    {
        $.ajax({
        url: "http://www.externaldomain.com/login/authenticate",  
        type: "POST",
        dataType: "json",
        contentType: "application/json",
        data: "{'emailAddress':'[email protected]', 'password':'Password1'}", 
        success: LoadUsersSuccess,
            error: LoadUsersError
        });         
    }
    function LoadUsersSuccess(){
        alert(1);
    }
    function LoadUsersError(){
        alert(2);
    }

However, when checking on Firebug, this brought up a 405 Method Not Allowed error.

At this stage, as this is the first time I've really worked with web services, I really just wondered whether this was the best way to do things? Is it worth persevering with this method in order to find a solution, or am I best to maybe try and find a server-side answer to this issue? If so, does anyone have any examples they could post up?

Many thanks

3 Answers 3

1

Doing cross-domain web service calls in a browser is very tricky. Because it's a potential security vulnerability, browsers block these types of requests. However, there is a workaround called JSONP. If you use JSONP instead of plain JSON, you should be able to make the cross-domain request.

Sign up to request clarification or add additional context in comments.

Comments

1

Right ok, to update where I am, I checked in firebug and on the external server and the reason why I'm getting a 405 error is because I'm doing a Get rather than a Post.

What I need to do is send the username and password, then receive a GUID back which will then be used for any future requests. I thought by having 'type:post' in the code would be enough but apparently not. Anyone know where I might be going wrong here? As I said, a novice to web services and nothing I have tried from looking online has had any effect. Many thanks

Comments

1

Ok, I got the problem solved, and I did it by going back to C# and doing it there instead of using jQuery or JSONP and I used Json.Net for handling the data received. Here is the code:

protected void uxLogin_Click(object sender, EventArgs e)
{
StringBuilder data = new StringBuilder();
data.Append("{");
data.Append("'emailAddress': '" + uxEmail.Text + "', ");
data.Append("'password': '" + uxPassword.Text + "'");
data.Append("}");

byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
string url = System.Configuration.ConfigurationManager.AppSettings["AuthenticationURL"].ToString();

string JSONCallback = string.Empty;
Uri address = new Uri(url);

// Create the web request  
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

// Set type to POST  
request.Method = "POST";
request.ContentType = "application/json";
// Create a byte array of the data we want to send  

// Set the content length in the request headers  
request.ContentLength = byteData.Length;

// Write data  
using (Stream postStream = request.GetRequestStream())
{
    postStream.Write(byteData, 0, byteData.Length);
}

// Get response  
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
    // Get the response stream  
    StreamReader reader = new StreamReader(response.GetResponseStream());

    // Console application output  
    JSONCallback = reader.ReadToEnd().ToString();
}


if (!String.IsNullOrEmpty(JSONCallback))
{
    JObject jObject = JObject.Parse(JSONCallback);
    if ((bool)jObject["loginResult"] == false)
    {
        string errorMessage = jObject["errorMessage"].ToString();
        int errorCode = (int)jObject["errorCode"];
    }
    else
    {
        string idToken = jObject["idToken"].ToString();
        Session["Userid"] = idToken;
        Response.Redirect("~/MyDetails.aspx");
    }
}
else
{
    uxReturnData.Text = "The web service request was not successful - no data was returned";
}

}

Thanks anyway :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.