JQuery and XHR - Cross-Site JSON POST with CORS

I am creating a small user interface for sending a JSON object to an external CastIron server (no matter what type of server is important for this issue) using jQuery. The initial submission is working fine, but I am not receiving a response from the server. Here's what jQuery looks like:

$.ajax({
        url: 'http://cirun2/Impact/CreateImpacts',
        type: "POST",
        data: JSON.stringify(myobj),
        dataType: 'text',
        async: false,
        //beforeSend: function(xhr){
        //      xhr.setRequestHeader(
        //},
        complete: function(returned_data) {
                $('#output').append("<p>Submitted successfully to CastIron. Returned data: " + returned_data + "</p>");
        },
        error: function(error_text) {
                console.log("Update unsuccessful. Status: ", error_text);
        }
});

I get 'Sent successfully in CastIron. Returned data: message [object Object]', but it does not display text, and firebug indicates an error.

Firebug response

And here is the complete error:

"[Exception ..." Failure "nsresult:" 0x80004005 (NS_ERROR_FAILURE) "location:" JS frame :: http://server.company.com/mr/js/jquery-1.11.0.min.js ::. send :: line 4 ": no]"

200 - JSON, . , . "" CastIron, , . , .

EDIT (7MAY2014): , , . CORS. . , , ?

Response Headers
Access-Control-Allow-Head...    X-Requested-With
Access-Control-Allow-Orig...    *
Connection  keep-alive
Content-Length  288
Content-Type    application/json; charset=utf-8
Date    Wed, 07 May 2014 14:34:51 GMT
X-Powered-By    Express

Request Headers
Accept  application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Host    myserver.mycompany.com:4000
Origin  http://ironsides.zayo.com
User-Agent  Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:28.0) Gecko/20100101 Firefox/28.0
+4
3

CastIron. CI-, "200 OK" , :

Access-Control-Allow-Origin: *    
Access-Control-Allow-Headers: X-Requested-With

, . CORS CORS Firebug, .

+4

()

$(function () {
    var callback = function (results) {    
        $("#output")
        .append("<p>Submitted successfully to server. Returned data: " 
        + JSON.stringify(results.caseURL) + "</p>")    
    };
    var myobj = {
        "caseURL": {
            "caseURL": "https://cs18.salesforce.com/50060000008ugtSAAQ"
        }
    };

    var request = $.ajax({
        url: "/echo/json/",
        data: {
            json: JSON.stringify(myobj)
        },
        type: "POST",
        dataType: "json"
    });
    request.done(function (data, textStatus, jqxhr) {
        if (textStatus === "success" && jqxhr.responseJSON) {
            console.log(data, jqxhr.responseJSON, jqxhr.responseText);
            if (data.hasOwnProperty("caseURL")) {
                callback(data)
            };
        };
    });
    request.fail(function (jqxhr, textStatus, error_text) {
        if (textStatus != "success") {
            console.log("Update unsuccessful. Status: ", error_text, 
            textStatus, jqxhr.getAllResponseHeaders());
        };
    });
})

jsfiddle

.

http://doc.jsfiddle.net/use/echo.html#json

http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings (data, dataFilter, processData)

http://api.jquery.com/jQuery.ajax/#sending-data-to-server

+3

POST /x -www-form-urlencoded, multipart/form-data text/plain CORS.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

See Sample Access Control Scripts> Simple Queries

Please correct me if you are mistaken.

0
source

Source: https://habr.com/ru/post/1539027/


All Articles