English 中文(简体)
Nodejs 请求发送
原标题:Nodejs request patch

I m currently trying to implement a patch request in my nodejs application. But I can t seem to get it to work. I have made sure my data structure is correct.

I ve tried some different things like: returning my request.patch and promise but nothing seem to happen.

var request = require( request );
exports.patchOwner = function (options) {
    var urlEncodedParams = encodeURI(options.nodeId);
    request.patch(url +  /owner/owners/  + urlEncodedParams, JSON.stringify(options.body), function (err, res, body) {
            //body is empty
    iii);

iii

问题回答

我试图这样做:

var fullUrl = url +  /owner/owners/  + encodeURI(options.nodeId);
var body = JSON.stringify(options.body);
request.patch({ url: fullUrl, body: body }, function (err, res, body) {
  // do your thing
})

页: 1 请求

server.js

app.patch("/data/:id/:patch", (q, r) => {
    let id=q.params.id;
    let id=q.params.patch;

    //your logic

    r.send({ res: "*PATCHED*" });
});

client.js

let ajax = (type, url, data, callback) => {
    let xhr = new XMLHttpRequest();
    xhr.open(/* GET */type, url, true/* ASYNC */);
    xhr.onload = () => callback(xhr.status === 200 ? JSON.parse(xhr.response).res : xhr.status);
    xhr.setRequestHeader( Content-Type ,  application/json );

    if (type === "POST")
        xhr.send(JSON.stringify(data));
    else
        xhr.send();//PUT - PATCH - DELETE - GET
}

usage

ajax( PATCH ,  /data/1/something_to_patch , null, (res)=>{alert(res)});

<代码>GET,PUTDELETE

POST:

server.js

app.post("/data", (q, r) => {
    let body_data = JSON.stringify(q.body);
    r.send({ res: "*POST: " + body_data + "*" });
});

<>strong>client; post data with js:

<script>
    let data = {
        "id": 1,
        "name": "AJAX_POST"
    }

    ajax( POST ,  /data , data, (res)=>{alert(res)});
</script>

client; post form:

<form action="/data" method="POST">
    <input name="id" type="hidden" value="2" />
    <input name="name" type="hidden" value="FORM_POST" />
    <input type="submit" value="FORM_POST" />
</form>




相关问题
PHP SoapClient request

I m trying to send a SOAP request to a newsletter service using this WSDL. Here s my PHP: $client = new SoapClient($wsdl_url, array( login => myusername , password => mypassword ,...

twitpic API not connecting (multipart/form-data) objective-c

I ve put together this code from lots of research. Information on the twitpic API is here: http://twitpic.com/api.do#uploadAndPost Through debugging i can tell that the dictionary is good, and ...

Rails - RESTful Routing - Add a POST for Member i.e(tips/6)

I m trying to create some nice RESTful structure for my app in rails but now I m stuck on a conception that unfortunately I m not sure if its correct, but if someone could help me on this it would be ...

grails + gwt request handling via controllers

I am new to gwt. I am trying to integrate gwt+grails.Can anybody provide me a good example for handling the request using grails controllers and not a custom servlet.I will be really thankful if ...

add text in http request string url

ok i made a midlet through which i can connect to server pages and get soem information as response. For example i made a midlet through which i acced url: http://example.com/?u=nepal&t=1 Now i ...

How to find if a request is for js or css in httpHandler

is there any way to find if a particular request is for JS or CSS in httphandler to improve the performance of my website i was using HttpCompress from Code Project http://www.codeproject.com/KB/...

How do I grab all the request parameters in Catalyst?

Specifically I m trying to capture all the POST parameters from a payment gateway as a single string, and then parse them looking for the string ERROR . I m aware that there s a $c->request->...

Fire off an internal ServletRequest in Tomcat

I am using Quartz to schedule background tasks for a web application. Some of these tasks just fire off requests against the same web application. I want to avoid depending on any kind of network ...

热门标签