Hi, You're setting the contentType of the *request* to application/json, but that's not what you're sending; what you're sending is URL-encoded data. The "contentType" option refers to your request, not the response.[1] It's up to the server to set the content type of the response.
> 2. onComplete occurs and the login_response function is sent - however > the response variable is null The response argument *itself* is null? I've never seen that happen, and looking at the Prototype code, I'm not seeing how it /can/ happen. Do you mean that response.responseJSON is null? That could certainly happen, if the server isn't returning the correct content-type on the response. If it's response.responseJSON that's null, what does response.responseText look like? If it contains the JSON string, you can set the evalJSON option on your request to 'force' to force Prototype to evaluate the response as JSON even if the server doesn't return the correct content type. Finally: You're using an onComplete handler and an onFailure handler. That means you'll get two alerts when things fail, first one from onFailure, then another one from onComplete, because onComplete happens regardless of what happens to the request. You probably want to change onComplete to onSuccess. You might find this article[2] on the unofficial wiki helpful. So what I'd suggest is: * Remove the contentType option from your request; the default is fine. * Change onComplete to onSuccess. * If that doesn't fix it, and the response argument is coming back but it's response.responseJSON that's null, add 'evalJSON: "force"' to your options. Hope one of those is it! [1] http://api.prototypejs.org/ajax/ [2] http://proto-scripty.wikidot.com/prototype:how-to-bulletproof-ajax-requests HTH, -- T.J. Crowder Independent Software Consultant tj / crowder software / com www.crowdersoftware.com On May 7, 1:06 am, hellboy1975 <[email protected]> wrote: > Hi Guys, > > I'm relatively new to AJAX and Prototype, so please be gentle with me. > > I've been working on getting what I expected was a simple AJAX request > into my site, and the code I've created looks right to me, however > only works in Internet Explorer. Initially I thought the problem > might be Prototype, but I quickly did the same thing in jQuery, and > that also fails in the same way. > > All the code is being run on localhost, and it's basically a PHP site > running with Smarty, communicating with a Web Service responding with > JSON. > > Firstly, the HTML: > > <html> > <head> > <title>VA PHP Test Site</title> > > <script type="text/javascript" src="scripts/prototype.js"></ > script> > <script type="text/javascript" src="scripts/va-login.js"></ > script> > > </head> > <body> > <h1>VA PHP</h1> > > <p>Please Log In:</p> > <form id="login-form"> > User: > <input type="text" name="Person" /> > <br /> > Password: > <input type="text" name="Password" /> > <input type="submit" value="Login" onclick="login_staff();" /> > </form> > > <div id="login-response"></div> > </body> > </html> > > And the Javascript: > > function login_response(response) { > var login = response.responseJSON; > alert("State: " + login.LoginState + "\nAccount: " + login.Account + > "\nUser: " + login.User + "\nPassword: " + login.User); > > } > > function login_staff() > { > var loginString; > var url='LoginWSO.wso/Login_Staff/JSON'; > > // this sets loginString to "Person=value1&Password=value2" > loginString = Form.serialize("login-form"); > > // creates and sends the request. > // pops an alert if there is an error, or fires login_response when > complete > var myAjax = new Ajax.Request( > url, > {method: 'POST', parameters: loginString, contentType: > 'application/json', > onComplete: function(response){alert('response ' + > response.responseJSON); }, > onFailure: function(){ alert('Something went > wrong...'); } > } > ); > > } > > Just to sum it it, while using the Chrome debugger I found the > following: > 1. login_staff is fired, and appears to collect the various settings > ok > 2. onComplete occurs and the login_response function is sent - however > the response variable is null > 3. As a result the alert appears, but is largely blank > > In Internet Explorer, the alert pops up, and contains the correct > response. > In Firefox the behaviour is much the same as Chrome, however if I put > a break point on, and take my time stepping through the code, the > correct alert does actually appear. If it let it run normally > however, it behaves the same as Chrome. > > My initial thoughts were that same origin policy, but given that it's > all on localhost, I'm not sure why this would be the case. > > Any suggestions gratefully appreciated! > > Thanks, > Matt > > -- > You received this message because you are subscribed to the Google Groups > "Prototype & script.aculo.us" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group > athttp://groups.google.com/group/prototype-scriptaculous?hl=en. -- You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en.
