开发者

$.post works but not $.get ? On a different domain?

I am stuck on an issue with reading data from a server. The data is stored as a csv string and i am using php to read the data.

Reading Data

<?php 
    header('Content-Type: text/plain'); 
    $csv = file_get_contents('string.csv'); 
    echo $csv; 
?>

$.ajax({ 
    type: 'GET', 
    url: 'http://www.foobar.com/csv.php',
    async: false,
    data: null, 
    success: function(text) { 
        sv_serverArray 开发者_JAVA技巧= text.split(",");
        alert(sv_serverArray);
    } 
});

The Ajax call is done on the domain http://www.example.com the php file is served on http://foobar.com/csv.php

When i post the data from http://www.example.com to http://www.foobar.com/write.php it works! But not the other way around.

Writing Data

<?php
    $list = $_POST["array"];
    $fp = fopen('string.csv', 'w');
    fputcsv($fp, $list);
    fclose($fp);
?>

$.post("http://www.foobar.com/write.php", { 'array': sv_defaultArray});

What is the issue and why can i only write and not read ?! if anything i should be getting errors the other way around !!


The $.post() generates a underwater, and uses that to post. When posted it is a real request. The $.get() uses a XMLHttpRequest, which is bound by the Same-Origin Policy. The best way to circumvent this is jsonp. (either convert the CSV to json, or encapsulate it).


You can't do Cross Site Scripting in this way for security purposes

Read this: Cross domain AJAX querying with jQuery


Check the cross domain solutions here..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜