发布请求中的URL(js)

时间:2017-01-23 18:56:10

标签: javascript php post

我有一张表格:

    <form>
        <input id = "sendLink" type = "text" placeholder = "insert a link here">
    </form>

然后我尝试发送数据(它是一个像http://sameURL.com这样的网址(字符串)):

        xmlhttp.open('POST', 'http://host:8453/page.htm', true);
        xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        xmlhttp.send($link);

但是在服务器端,我收到了一个“分裂”的帖子:

"headers": {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    ...
    more headers here
    ...
    "User-Agent": "Mozilla/5.0"
},
"httpVersion": "1.1",
"method": "POST",
"post": {
    "http://sameURL.com": ""
},
"postRaw": "http://sameURL.com",
"url": "/page.htm"

}

为什么我

http://sameURL.com”:“”

在帖子正文中而不是

http://sameURL.com

?谢谢。我需要没有额外冒号和“”的请求。 我应该只使用“http://sameURL.com”字符串。

UPD 1

测试目的: $ link =“https://google.com?q1=asdf?q2=ddd?q3=ppp”;

在那种情况下,我们得到了:

"post": {
        "https://google.com?q1": "asdf?q2=ddd?q3=ppp"
},
"postRaw": "https://google.com?q1=asdf?q2=ddd?q3=ppp",

2 个答案:

答案 0 :(得分:0)

原因是您没有正确发送数据。

看看这个:https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send

UDP 1:

"https://google.com?q1": "asdf?q2=ddd?q3=ppp"

您没有&个符号来发送多个键值对。另外,请了解这些请求中通常如何发送和接收数据。

如果我是你,我会做以下事情:

$link = "link=https://google.com&q1=asdf&q2=ddd&q3=ppp";

我还没有对它进行过测试,但它应该导致:

"post": {
    "link": "https://google.com,
    "q1"  : "asdf",
    "q2"  : "ddd",
    "q3"  : "ppp"
}

原始问题

如果是原始网址:

$link = "link=http://sameURL.com";

会显示:

"post": {
    "link": "http://sameURL.com"
}

答案 1 :(得分:0)

您正在发送一个标题,说明数据是表单编码的,因此服务器需要键/值对。

我认为你应该尝试普通而不是What is the difference between form-data, x-www-form-urlencoded and raw in the Postman Chrome application?

xmlhttp.setRequestHeader('Content-type', 'text/plain');
相关问题