2011-02-11 33 views
0

我有原型JS Ajax請求,基本上是這樣的:翻譯prototypejs Ajax請求的libcurl在C++

new Ajax.Request("http://www.example.com", { 
    method: 'post', 
    postBody: '{"params":{"first":"one", "second":"two"}}', 
    contentType: 'text/json' 
    requestHeaders: { 
     'Content-Length': 42 
    } 
    onComplete: function() {} 
}); 

我試着libcurl中像這樣的:

struct curl_slist *slist = NULL; 
slist = curl_slist_append(slist, "Content-Type: text/json"); 
curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com"); 
curl_easy_setopt(curl, CURLOPT_HEADER, slist); 
curl_easy_setopt(curl, CURLOPT_POST, 1); 
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 42); 
curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, "{\"params\":{\"first\":\"one\", \"second\":\"two\"}}"); 

但在在服務器端,它不像原型實現那樣識別後置主體。

我還沒有能夠取得任何進展,所以我希望這裏有人能夠提供我需要的見解。謝謝!

+0

有`,`在缺少你AJAX請求(在onComplete和requestHeader之前) – giraff 2011-02-21 08:22:37

回答

0

我懷疑Prototype檢測到postBody是用JSON編寫的,並在將它發送到服務器之前轉換爲URL-Encoding(而libcurl沒有)。你可以安裝Firebug來看看會發生什麼。

嘗試發送以下內容作爲postBody/libcurl中:

params[first]=one&params[second]=two 
+0

Prototype將`postBody`視爲一個字符串,根本不處理它,只是將它[直接傳遞給XHR對象](https://github.com/sstephenson/prototype/blob /1fb9728/src/ajax/request.js#L212)。如果需要,檢查[`parameters`](http://api.prototypejs.org/ajax/)並將其轉換爲URL編碼的字符串。 – clockworkgeek 2011-02-21 12:47:03

0

另一個想法是添加一個原型sends by default到libcurl的標題:

X-Requested-With: XMLHttpRequest 
X-Prototype-Version: (Prototype.Version) 
Accept: text/javascript, text/html, application/xml, text/xml, */*