Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: parsererror with XmlHtpp-post-request



Johnny's code here is an example of a widget that should probably be updated in time for Leopard due to the change I alluded to earlier. I say 'probably' because the server he's communicating with may be very lenient and not care what content-type header value he's sending even though his request body is name=value pairs. But I would actually recommend the change either way, to be safe, and to be correct.

Again, Tiger default content-type from XMLHttpRequest:
"application/x-www-form-urlencoded"

Leopard default content-type from XMLHttpRequest:
"application/xml"

I have seen this change break widgets when the server is actually paying attention to what your widget is telling it. Keep in mind that this change in default behavior has been made in the pursuit of greater compatibility with other browsers, and is therefore, very important and necessary.

So Johnny, I would recommend adding the following line to the next shipped version of your widget. This change is compatible with Tiger, and will ensure it continues to work with Leopard:

post.open("POST", url, false);
post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
post.send(variables);


Todd Ditchendorf
Software Engineer



On Aug 28, 2006, at 12:21 PM, Johnny Storm wrote:

This time I'll send it to the list:

Here is post code that works in my widget, notice I am manually
populating the "variables" var which is being sent

function submitForm(theForm, div, url) {
var variables = "version="+version+"&";
for(i=0;i < theForm.length; i++){
if(theForm[i].type == 'radio' || theForm[i].type == 'checkbox') {
if(theForm[i].checked == true) {
variables += theForm[i].name +"="+ theForm[i].value + "&";
}
} else if(theForm[i].type != "button") {
variables += theForm[i].name +"="+ theForm[i].value + "&";
}
}
var post = new XMLHttpRequest();
post.open("POST", url, false);
post.send(variables);
var myresponse = post.responseXML.documentElement;
if (response) {
document.getElementById("Prefs").innerHTML = "<div class='body'
align='left' style='padding-left:5px'>" + post.responseText +
"</div>";
} else {
document.getElementById("Prefs").innerHTML = "<div class='body'
align='left' style='padding-left:5px'>An error has occured.</div>";
}
}

On 8/28/06, Todd Ditchendorf <email@hidden> wrote:

Hmm... if you're not running Leopard, then the situation I described below
is probably not affecting you.

However, there are some changes I would recommend you make to your
_javascript_. I can't seem to find any docs on this at the moment, but I'm
pretty certain that you must call open() before you call setRequestHeader()
for that method to take any effect. So I don't think your calls to
setRequestHeader() are having the desired effect.

Also, I would probably trust WebKit to get the content-length header right
rather than setting it manually. Especially since it sounds like the error
you are getting involves the length of the request, but who knows?


xmlhttp.open('POST', myUrl, true);

xmlhttp.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");

That apple doc is really just meant to be an intro to the XMLHttpRequest
object (and it's a very good one IMHO). For more a detailed spec, you might
try the new W3C working draft:


Hope that helps.




Todd Ditchendorf
Software Engineer




On Aug 28, 2006, at 11:27 AM, Stephan Huebner wrote:

Hi Todd,

On 28.08.2006, at 18:54, Todd Ditchendorf wrote:


Are you running your widget on a Leopard developer seed or on Tiger?

No, I am not one of those fortunate ones :-)


req.open("POST", "http://www.example.com");
req.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
req.send(postData);

If I understand right, this is the way it should be done in Tiger, right?
And I did it exactly this way. Here's a snippet of what I wrote:

xmlhttp = false;
myRequest = "Param1=" + myP1 + "&Param2=" + myP2 + "&Param3=" + myP3;
xmlhttp = new XMLHttpRequest();
xmlhttp.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", myRequest.length );
xmlhttp.open('POST', myUrl, true);
xmlhttp.>
   ...
}

xmlhttp.send(myRequest);


See docs below for more XMLHttpRequest info:


Speaking about this doc, I have to say that it seems quite incomplete to me,
especially as a description of post-requests is missing. That should be
updated by Apple, I would say.

I've tried it again and what I got back was the document,containing this
text:

This page contains the following errors:
error on line 1 at column 1: Extra content at the end of the document
Below is a rendering of the page up to the first error.

Just to mention it, I also tried to setrequestheader with "application/xml"
with the same result.

Greetings,

Stephan




On Aug 26, 2006, at 12:37 AM, Stephan Huebner wrote:


Hi,

it's most definitely no _javascript_-error as when I try to send out the
post-request I get back a small html-document that contains just some ttags
and a text saying something about a "parsererror". I almost believe it is an
error-message generated by webkit or whatever it is that is responding to
the request. Though I don't know what could have generated this
error-message as I tried out a few post-request-examples from the internet
and did exactly what they did for generating the request.

Greetings,

Stephan

On 25.08.2006, at 20:50, Russ White wrote:


what is giving you the parse error? _javascript_? or xmlhttprequest?

On Aug 25, 2006, at 9:24 AM, Stephan Huebner wrote:


Hi all,

I'm new to this list but not to widget-programming in general. And I'm from
Germany, so my Englisch might sound a bit strange sometimes.

So, does anybody know why a post-request using XmlHttpRequest could result
in a parsererror? Normal get-requests do work fine, but post-request do not
though I think I did everything required including setting of headers for
parameters like mime-type, length of data that will be sent with the
post-request and so on. I am not sure if is a problem with the post-request
itself or maybe with the url I use (maybe the result isn't well-formed or
something so that might lead to an parsererror?

Btw, I get the same error if I disconnect the internet, so it rather seems
as if this is an error-message built into safari or webkit, but even if, I
still don't know what causes this error.

Greetings and thanks in advance,

Stephan Huebner

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Dashboard-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden



--
Love: The only and satisfying answer to the existance of human being
-- Sigmund Freud


_______________________________________________
Do not post admin requests to the list. They will be ignored.
Dashboard-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden



--
Fanatiker tragen die Humorlosigkeit als Vereinsabzeichen - unbekannt





 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Dashboard-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


_______________________________________________
Do not post admin requests to the list. They will be ignored.
Dashboard-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Dashboard-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/dashboard-dev/email@hidden

This email sent to email@hidden

References: 
 >parsererror with XmlHtpp-post-request (From: Stephan Huebner <email@hidden>)
 >Re: parsererror with XmlHtpp-post-request (From: Russ White <email@hidden>)
 >Re: parsererror with XmlHtpp-post-request (From: Stephan Huebner <email@hidden>)
 >Re: parsererror with XmlHtpp-post-request (From: Todd Ditchendorf <email@hidden>)
 >Re: parsererror with XmlHtpp-post-request (From: Stephan Huebner <email@hidden>)
 >Re: parsererror with XmlHtpp-post-request (From: Todd Ditchendorf <email@hidden>)
 >Re: Re: parsererror with XmlHtpp-post-request (From: "Johnny Storm" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.