如何为此POST编写ajax调用

时间:2015-10-13 12:37:44

标签: javascript ajax post

我还没有参与过AJAX,我曾尝试过ajax函数在curl下面发布这个,

curl -X POST -H 'Content-type:application/json' --data-binary '{
 "replace-field":{
 "name":"sell-by",
 "type":"date",
 "stored":false }
}' http://localhost:8983/solr/gettingstarted/schema

我已根据SF中可用的资源编写了以下功能,但这并未将数据发布到应用程序。它也不会抛出任何控制台错误。

 function sendData() {
    $.ajax({
        url: 'http://localhost:8983/solr/techproducts/schema',
        headers: {
            "Access-Control-Allow-Origin":"*"
        },
        type: 'POST',
        contentType: 'application/json',
        data: JSON.stringify({
              "replace-field":{
                     "name":"sell-by",
                     "type":"date",
                     "stored":false
                     }
                }),
       success: function (response, status, xhr) {                         
                    if (status == "success") {
                        alert("sucess");
                    } else {
                        alert(status + ' - ' + xhr.status);
                    }
                },
        // processData: false, // this is optional
        dataType: 'jsonp',
        jsonp: 'json.wrf',
        crossDomain: true,
    });
}

1 个答案:

答案 0 :(得分:0)

function sendData() {
    $.ajax({
        url: 'http://localhost:8983/solr/techproducts/schema',
        headers: {
            "Access-Control-Allow-Origin":"*"
        },
        type: 'POST',
        contentType: 'application/json',
        data: {
              "replace-field":{
                     "name":"sell-by",
                     "type":"date",
                     "stored":false
                     }
                },
                success: function (response, status, xhr) {                   

                    if (status == "success") {
                        alert("sucess");

                    } else {
                        alert(status + ' - ' + xhr.status);
                    }
                },
        // processData: false, // this is optional
        dataType: 'jsonp',
        jsonp: 'json.wrf',
        crossDomain: true,
    });
}