NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum

Hello community,

I am trying to create POI nodes from javascript using AJAX requests (via a proxy) to talk to the API. My code to create a node looks like this:

function osmNode(action, data){

    var url =  host + "api/node.php";

    var params = new Object();
    params["action"] = action;
    params["changeset_id"] = changeSetId;
    params["node_id"] = nodeId;
    params["comment"] = commentId;
    params["userName"] = userName;
    params["userPassword"] = pwd;
    params["data"] = data;

    document.getElementById("saving").style.visibility = "visible";

    $.ajax({

        url: url,
        type: "GET",
        data: params,
        success: function(reqCode){

            var response = reqCode;
            switch (action){
                case "create":
                    nodeId = trim(response);
                case "move":
                case "update":
                case "delete":
                    updateOSM();
                    break;
            }
            document.getElementById("saving").style.visibility = "collapse";
            return "0";
        }
    });
}

I call this with an action of "create" and the following XML for the node:

<?xml version='1.0' encoding='UTF-8'?>
<osm version="0.6" generator="OpenStreetMap server"> 
<node id="-1" changeset="8151" version="null" lat="51.97096" lon="7.70825">
<tag k="osmeditor:name" v="Foo"/>
<tag k="osmeditor:note" v="Bar"/>
</node>
</osm>

and it talks to my proxy which in turn talks to the API but instead of getting a new node ID back I get a message which reads:

</osm>. Fatal error: String not started expecting ' or " at :1.

Do you know this error ? Can anyone help ?

If you need to see the site, it is at http://www.uni-pages.de/projects/osm/index.php and it is talking to the api06 test API.

greets Christian

asked 30 Jan '11, 16:36

paule85's gravatar image

paule85
16112
accept rate: 0%

edited 02 Feb '11, 00:57

TomH's gravatar image

TomH ♦♦
3.3k83943


I have stepped through your javascript in Firebug and traced the network traffic from my browser to your proxy, and from your proxy to the dev server and the problem seems to be with your proxy script.

Although the XML which leaves the browser looks like what you showed in the question, what arrives at the server looks like this:

<?xml version=\'1.0\' encoding=\'UTF-8\'?>
<osm version=\"0.6\" generator=\"OpenStreetMap server\"> 
<node id=\"-1\" changeset=\"8151\" version=\"null\" lat=\"51.97096\" lon=\"7.70825\">
<tag k=\"osmeditor:name\" v=\"Foo\"/>
<tag k=\"osmeditor:note\" v=\"Bar\"/>
</node>
</osm>

As you can see, your proxy has escaped all the quotes. That causes the XML to fail to parse and the server to return this error:

Cannot parse valid node from xml string <?xml version=\'1.0\' encoding=\'UTF-8\'?>
<osm version=\"0.6\" generator=\"OpenStreetMap server\"> 
<node id=\"-1\" changeset=\"8151\" version=\"null\" lat=\"51.97096\" lon=\"7.70825\">
<tag k=\"osmeditor:name\" v=\"Foo\"/>
<tag k=\"osmeditor:note\" v=\"Bar\"/>
</node>
</osm>. Fatal error: String not started expecting ' or " at :1.

Unfortunately it looks like your proxy only returns the last line of that to the browser, hence the confusing error message in the browser.

I would also add that passing the XML as a URL parameter to the proxy is not really a good idea - it would be better to use POST and pass it in the body. Indeed as creating a node is not idempotent you should be using POST rather than GET for that request anyway.

permanent link

answered 02 Feb '11, 01:01

TomH's gravatar image

TomH ♦♦
3.3k83943
accept rate: 20%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×92
×60
×10

question asked: 30 Jan '11, 16:36

question was seen: 16,376 times

last updated: 02 Feb '11, 01:01

NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum